From: David Whetstone Date: 2009-08-11T22:42:50+09:00 Subject: Re: instance_variables vs. local_variables Brian Candler wrote: > As far as I understand, it's not exactly a "copy". Rather, each binding > is a linked list of frames, and if you create a new binding it's the > same linked list but with an empty frame on top. So if you create a new > variable it goes in the top frame, but if you are searching for an > existing variable it hunts back along the list. That makes sense. >> So, >> there really is no way to affect the bindings of an existing context, >> since only a copy of the current bindings can ever be acquired. > > No, because you can pass a binding around as an object, which means you > can manipulate it even when it isn't the 'current' binding: > > def define_a(b) > eval "a=1", b > end > > define_a(binding) > puts local_variables.inspect # prints ["a"] > > You can also implicitly do this using the binding of a block: > > def another_a(&blk) > eval "a=1", blk.binding > yield > end > > another_a do > puts local_variables.inspect # prints ["a"] > end But these examples completely contradict the previous discussion. I was surprised by them, because if they worked, I would have never made my original post. But it turns out you are right, they do work ... in Ruby 1.8. I should have prefaced that I am using Ruby 1.9. Apparently, there must have been a change to how bindings work between the two versions, because neither example works in Ruby 1.9. -- Posted via http://www.ruby-forum.com/.