From: Joel VanderWerf Date: 2005-10-10T06:31:00+09:00 Subject: Re: using lambda/Proc can prevent a lot of garbage collection Eric Hodel wrote: > A closure should only enclose the variables needed for its execution. > This is actually not very difficult but unfortunately eval('a') prevents > Ruby from excluding unbound variables in closures. > > This is a (unfortunate IMO) feature of Ruby. > Aren't there some useful hacks that depend on being able to get at vars in the caller's scope (which need not be referenced in the block), using the eval str, some_proc construct? I use it in my observable lib to get the self of the block's context, using something like this: def when_#{var} pattern=Object, &block observer_map = @#{var}__observer_map ||= ObserverMap.new if block observer = eval "self", block to find out what object is observing a variable. But that's not as objectionable as getting access to arbitrary local vars in that context. Does anyone remember--is there is anything that uses the latter kind of access (arb. local vars) that is more than just a cute hack? Is there anything really useful we would lose without that behavior? -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407