From: Martin DeMello Date: 2006-08-25T02:57:29+09:00 Subject: Re: do not understand this closure On 8/24/06, Dave Rose 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) > => # > >> cube = power(3) > => # > >> p square > # > => 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