Converting a Proc-object into a normal method.

Discussion in 'Ruby' started by Michael Lesniak, Jun 15, 2006.

  1. 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
    Michael Lesniak, Jun 15, 2006
    #1
    1. Advertising

  2. Michael Lesniak

    Trans Guest

    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.
    Trans, Jun 15, 2006
    #2
    1. Advertising

  3. 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
    Robert Klemme, Jun 15, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi
    Replies:
    3
    Views:
    1,920
    David Lozzi
    Jun 1, 2005
  2. NevilleDNZ
    Replies:
    9
    Views:
    427
    NevilleDNZ
    Aug 16, 2006
  3. Jean-Hugues ROBERT

    Why no Proc##[]=() ? Why no Proc##replace() ?

    Jean-Hugues ROBERT, May 1, 2004, in forum: Ruby
    Replies:
    14
    Views:
    269
    Jean-Hugues ROBERT
    May 5, 2004
  4. Minkoo Seo

    Proc vs lambda vs proc

    Minkoo Seo, Feb 4, 2007, in forum: Ruby
    Replies:
    19
    Views:
    231
    Brian Candler
    Feb 6, 2007
  5. makoto kuwata
    Replies:
    1
    Views:
    634
    Robert Klemme
    Apr 22, 2012
Loading...

Share This Page