Forum: Ruby Proc&Bloc

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
00906171eb1cfc6958b6c634198f2dc9?d=identicon&s=25 Yaya Sow (ruby_afrique)
on 2007-04-11 19:43
I start learning ruby since a week but i have some probleme to undestand
and the philosophy which hides behind .
Why don't using methode ?
In this code vhere is the Proc?

        def gen_times(factor)
          return {|n| n*factor }

        times3 = gen_times(3)
        times5 = gen_times(5)               #=> 36                #=> 25   #=> 60

Really it do not arrive to undertand
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-04-11 20:27
(Received via mailing list)
Try your code again in irb.
then, in irb

Should give you the answer.


When you def gen_times, actually, you are telling how to create an
instance of Proc with a code block. gen_times's code block happens to
take the factor and multiply it by n
this is like a function object.

If you've only been at it one week, don't worry. It will become clear!
00906171eb1cfc6958b6c634198f2dc9?d=identicon&s=25 Yaya Sow (ruby_afrique)
on 2007-04-11 23:55
Hi Thank
I see what is the probleme

irb(main):001:0> def gen_times(factor)
irb(main):002:1>    return {|n| n*factor }
irb(main):003:1> end
=> nil
irb(main):004:0> times3 = gen_times(3)
=> #<Proc:0x0c373dac@(irb):2>
irb(main):005:0> times5 = gen_times(5)
=> #<Proc:0x0c373dac@(irb):2>
=> 36
=> 25
=> 60
This topic is locked and can not be replied to.