On 8/24/06, Dave Rose <bitdoger2 / yahoo.com> wrote:
> in The Ruby Way...chap 1...a crude example of closure doesn't work as
> this irb
> session:
> >> def power(exponent)
> >>  proc {|base| base**exponent}
> >> end
> => nil
> >> square = power(2)
> => #<Proc:0x02dc58f0@(irb):2>
> >> cube = power(3)
> => #<Proc:0x02dc58f0@(irb):2>
> >> p square
> #<Proc:0x02dc58f0@(irb):2>
> => nil
> >> square(11)
> NoMethodError: undefined method `square' for main:Object
>         from (irb):7

You want square[11] or square.call(11)

square(11) is interpreted as "call the method 'square' on the current
"self" object, with the argument '11'", whereas square[11] is
interpreted as "call the method [] on the object 'square' with the
argument 11. Proc defines [] to be a synonym of call.

martin