Robert Dober
7/1/2007 10:27:00 AM
On 7/1/07, Rishel,Wes <Wes.Rishel@gartner.com> wrote:
> I am a Python duffer who got interested in Rails an decided to check out
> Ruby.
Welcome to the jewel.
Local variables spring into existance in the scope they are assigned to
Our friend irb shows this quite nicely
irb(main):008:0> %w{ word=2 word }.each{|c| p eval( c )}
2
NameError: undefined local variable or method `word' for main:Object
from (irb):8
from (irb):8:in `eval'
from (irb):8
from (irb):8:in `each'
from (irb):8
from :0
irb(main):009:0> word=nil
=> nil
irb(main):010:0> %w{ word=2 word }.each{|c| p eval( c )}
2
2
=> ["word=2", "word"]
eval creates a scope of it's own, thus eval("word=2") brings it into
life and it sadly dies.
But when eval("word=2") assigns to the already existing local variable
in the outer scope it persists into the next eval.
And just a friendly hint, forget eval you *do not need it* :).
HTH
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck