From: Glenn Parker Date: 2005-06-02T21:33:29+09:00 Subject: Re: binding, ObjectSpace._id2ref [WAS] Re: [ANN] parseargs-0.0.0 Zev Blut wrote: > > If you cannot access a in your binding, why would eval add a to the > local_variables? Can anyone explain this? I would like to know. It is a curious side effect of the way Ruby is interpreted. Perhaps it is a performance optimization? It doesn't really have to be this way, but it won't affect you (much) in practical terms. Variables that are "visible" when a method is compiled are added to a table somewhere in the AST, and only those variables can be accessed directly in that method. Clearly, this table is not the same one that "eval" consults so we have, to put it diplomatically, a little disconnect. Eval can find the variable when asked, but the interpreter simply refuses to go looking for it in the first place. Now, ask yourself if you really care. Are you going to be writing code where you know the name of a variable ahead of time, but only initialize it inside an eval statement? If so, there are two simple workarounds: 1. Initialize the variable before the eval. x = nil # make "x" visible beforehand eval "x = 1" print "#{x}\n" 2. Assign the eval-initialized variable to a "visible" variable. eval "y = 2" y = eval "y" # make "y" visible afterwards print "#{y}\n" -- Glenn Parker | glenn.parker-AT-comcast.net |