Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Converting a Proc-object into a normal method.

Reply
Thread Tools

Converting a Proc-object into a normal method.

 
 
Michael Lesniak
Guest
Posts: n/a
 
      06-15-2006
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

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      06-15-2006

Michael Lesniak wrote:
> 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.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-15-2006
Michael Lesniak wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
login into normal web pages through c#.net apondu ASP .Net 3 02-28-2008 06:09 AM
.NET-ey way to convert XML-encoded/escaped entities into normal characters/HTML? ASP .Net 2 06-20-2007 05:32 PM
converting a text file into an "insert into ..." file kublaikhan55@hotmail.com Ruby 5 07-23-2006 07:35 PM
How do I get back into my "normal" account? emanuel.levy@gmail.com Computer Support 3 03-12-2006 12:57 PM
Trouble printing QuarkXpress into postscript drive as prn and converting into pdf files using distiller OZ Computer Support 0 09-27-2004 05:03 PM



Advertisments