On Tue, Aug 01, 2006 at 11:43:32AM +0900, Logan Capaldo wrote: > On Jul 31, 2006, at 10:18 PM, Chad Perrin wrote: > > >def foo > > lambda { x = 1; puts eval("X".downcase) } > >end > > > > > It's still a closure. > > Like I said it's hard to give you an example, but ruby creates a > closure for a block regardless. That lambda still has a reference to > the enclosing scope of foo, even if their are no variables there. Ok > I came up with a good one: > > % cat closure2.rb > class A > def initialize > @a = 1 > end > > def foo > lambda { puts @a } > end > > def set_var(val) > @a = val > end > end > > a = A.new > baz = a.foo > baz.call > > a.set_var( "Hello" ) > > baz.call > > % ruby closure2.rb > 1 > Hello That block, "lambda { puts @a }", is still making reference to a variable that was explicitly declared to a scope outside itself. Let's try this: def foo bar = 0 lambda { bar += 1; puts bar; } end baz = foo There's a closure. Invoking baz.call will output an iterated value. Now have a look at this: def foo puts "Stuff!" lambda { puts "Junk!" } end bar = foo . . . then tell me whether it's still a closure -- and why, despite the lack of reference within the lambda block to anything outside of it. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] This sig for rent: a Signify v1.14 production from http://www.debian.org/