From: David Vallner Date: 2006-01-12T00:28:33+09:00 Subject: Re: Stupid question from a Ruby newb gwtmp01@mac.com wrote: > > Is this because the Smalltalk compiler will notice that there are no > references to variables outside the block and Ruby's current > implementation > will not notice the same thing? > > > Gary Wright > Quite so, Smalltalk compilers could optimize blocks that obviously didn't close upon the lexical environment by dropping part or all of the lexical binding. There was a thread about procs being a potential memory leak on the list that discussed this in Ruby, you might want to look at the ML archives. One of the arguments raised in the thread was that this optimization isn't really in Ruby because of eval. When blocks are called, they have to be (usually) executed in the lexical scope in which they were defined. The same is true of eval. Unfortunately, it's harder to determine what in the surrounding scope is referenced in an eval call than in a block, since the code is parsed at runtime; also you can't really determine when eval is called because of aliasing. Therefore optimizing the blocks would very likely break the use of eval in them. I don't know what conclusion the original discussion came to, I'd personally like to see eval mildly deprecated in favor of the many other, more explicit, and cleaner reflection capacities like #class_eval and #define_method for code generation, discouraged for use in the standard library, and optimizing the blocks as optional interpreter / VM. David Vallner