From: Chad Perrin Date: 2006-08-03T00:49:11+09:00 Subject: Re: I thought this was the one that worked? On Wed, Aug 02, 2006 at 10:59:15PM +0900, Pit Capitain wrote: > Chad Perrin schrieb: > >In any case, this can happen in Ruby: > > > > class Foo > > def bar > > lambda { puts "quux" } > > end > > end > > > > foo = Foo.new > > bar = foo.bar > > > > bar.call > > Chad, I didn't carefully read all of this long thread because I've been > offline for a couple of days. You don't have to answer to this mail, if > it isn't relevant to you. > > I had the impression that you think a block can't be called a closure if > it doesn't reference any free variables from its context, just as the > block in the example above. If I understand you correctly, then the > following two blocks should be exactly the same, shouldn't they? > > blk1 = Foo.new.bar > blk2 = Foo.new.bar > > But they aren't the same, as can be seen from the following code: > > eval "self", blk1 # => # > eval "self", blk2 # => # > > As you can see, each block remembers at least the value of "self", when > it was created. You can get at this value even if the "self" object goes > out of scope, as you can see from my code above. There's no explicit > reference to the two instances of Foo. From this it follows (at least > for me), that each block in Ruby is a closure. While I've reached a point where I think I agree that all blocks can be called closures according to a particular valid (liberal) definition of "closure", I don't think your explanation holds water as it stands. The problem is that maintaining state is not the only requirement for a closure. In fact, according to the interpretation of a closure's definition that allows all blocks in Ruby to be closures, a closure need not maintain (meaningful) state at all. Besides, I think you're pointing out memory addresses for those objects, not something internal to them. Please correct me if I'm wrong. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] Ben Franklin: "As we enjoy great Advantages from the Inventions of others we should be glad of an Opportunity to serve others by any Invention of ours, and this we should do freely and generously."