--- Joel VanderWerf <vjoel / path.berkeley.edu> wrote:

> 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.

I assume you mean "eval str, some_binding" and 'eval "self",
block.binding'.

I've used this before too.  This is kind of a way to get the
Binding#of_caller.  I was thinking of this case when I made
this suggestion:

3. Proc#binding should hold weak references to variables that
the block doesn't access (at compile-time).

So in the above case, "self" would still be available
regardless of whether the block referenced self in the code
because there is no way it would be freed yet (it is in the
context of the caller) and Proc#binding would still have this
weak reference.  "self" is a special case "variable", but it
should apply to more generic variables too.



	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com