From: Gennady Bystritsky Date: 2006-08-02T00:56:02+09:00 Subject: Re: I thought this was the one that worked? Chad, Sorry for jumping in late in the game, however I think this can explain why a block is a closure even when it does not seemingling reference any variables from the scope it was created in: class A def f proc { puts "abc" } end end a = A.new b = a.f b.call class << a def puts(*args) $stderr.puts args.map { |_a| _a.upcase } end end b.call Procuces: abc ABC Closure references not only variables, but anything else in the scope, like methods, etc. And the redefined method may reference variables in that scope, that's why the "full blown" closures are created no matter what the block [currently] references. Gennady. > -----Original Message----- > From: Chad Perrin [mailto:perrin@apotheon.com] > Sent: Tuesday, August 01, 2006 12:45 AM > To: ruby-talk ML > Subject: Re: I thought this was the one that worked? > > On Tue, Aug 01, 2006 at 01:21:01PM +0900, Logan Capaldo wrote: > > On Aug 1, 2006, at 12:06 AM, Chad Perrin wrote: > > >On Tue, Aug 01, 2006 at 12:47:50PM +0900, Logan Capaldo wrote: > > >> > > >>In ruby this environment always exists for a block > regardless of the > > >>presence of free variables, since the declaration of the variables > > >>that are free in the block can be added after (in time) > the creation > > >>of the closure. > > > > > >I think that depends on what you mean by "creation of the > closure" in > > >this case. Are you talking about the code used to create > it, or the > > >assignment of the return value of the closure's enclosing > scope to a > > >variable? > > > > > > > I'm talking about what happens inside the ruby interpreter when it > > sees that code. As far as ruby is concerned, it doesn't > know how to > > make a block that isn't a "closure". The same code path > gets followed > > whether ruby sees > > > > def foo > > a = 1 > > lambda { ... a ... } > > end > > > > or > > > > def foo > > lambda { ... } > > end > > > > All the "closure bookkeeping" is done whenever you create a > block. No > > extra bookkeeping is done whether or not you take advantage of the > > fact that you have a "closure". > > Arrrrrrgh. We're talking past one another. I can see where at least > some of the disconnect is occurring, but I don't seem to be > able to get > across to you what you're not saying so that you'll explain it. > > I give up for now. I'll just see if I can learn what's actually going > on with blocks and closures in Ruby from some other source. > > -- > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] > "There comes a time in the history of any project when it > becomes necessary > to shoot the engineers and begin production." - MacUser, November 1990 > >