Converting a Proc-object into a normal method.

M

Michael Lesniak

Hello,

on my way through dynamic method creation another question arose:

--- snip ---
def gen_times(factor)
return Proc.new {|n| n*factor }
end

times3 = gen_times(3)
times3.call(12)
--- snap ---

This example from the ruby-book creates a function on the fly which I
quite what I searched for. From my little scheme background I supposed
that I can directly bind the return value of gen_times to a variable
which is then equal to a method call. But, as exemplified above, I have
to go through call(...). Is there any way to convert a Proc-object to a
normal method, e.g. I would only have to do

new_times3 = gen_times(3).magic_call

and could then just write

new_times3(12)


Thanks for helping,
Michael
 
T

Trans

Michael said:
Hello,

on my way through dynamic method creation another question arose:

--- snip ---
def gen_times(factor)
return Proc.new {|n| n*factor }
end

times3 = gen_times(3)
times3.call(12)
--- snap ---

This example from the ruby-book creates a function on the fly which I
quite what I searched for. From my little scheme background I supposed
that I can directly bind the return value of gen_times to a variable
which is then equal to a method call. But, as exemplified above, I have
to go through call(...). Is there any way to convert a Proc-object to a
normal method, e.g. I would only have to do

new_times3 = gen_times(3).magic_call

and could then just write

new_times3(12)

Not extactly as you eg'd, but:

def magic_call( name, &proc )
define_method( name, &proc )
end

T.
 
R

Robert Klemme

Michael said:
Hello,

on my way through dynamic method creation another question arose:

--- snip ---
def gen_times(factor)
return Proc.new {|n| n*factor }
end

times3 = gen_times(3)
times3.call(12)
--- snap ---

This example from the ruby-book creates a function on the fly which I
quite what I searched for. From my little scheme background I supposed
that I can directly bind the return value of gen_times to a variable
which is then equal to a method call. But, as exemplified above, I have
to go through call(...). Is there any way to convert a Proc-object to a
normal method, e.g. I would only have to do

new_times3 = gen_times(3).magic_call

and could then just write

new_times3(12)

irb(main):009:0> def gen_times(sym,fact)
irb(main):010:1> class <<self;self;end.instance_eval do
irb(main):011:2* define_method(sym) {|x| x*fact}
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> gen_times :foo, 3
=> #<Proc:0x003780f8@(irb):11>
irb(main):015:0> foo 10
=> 30

If you want to work like in functional languages it's probably easier to
use lambdas only:

def curry_1(*args, &b)
lambda {|*a| b[*(args+a)]}
end
def curry_2(b, *args)
lambda {|*a| b[*(args+a)]}
end

irb(main):025:0> m3 = curry_1(3){|x,y| x*y}
=> #<Proc:0x100e9fa8@(irb):17>
irb(main):026:0> m3[10]
=> 30
irb(main):027:0> m3 = curry_2 lambda {|x,y| x*y}, 3
=> #<Proc:0x003c68c0@(irb):20>
irb(main):028:0> m3[10]
=> 30

Kind regards

robert



Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top