From: Robert Dober Date: 2007-07-01T19:27:20+09:00 Subject: Re: Newbie Question. eval.rb variable scope issue On 7/1/07, Rishel,Wes 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