From: daz Date: 2003-06-25T15:55:23+09:00 Subject: Re: Another Run Time Evaluation Q: "Shashank Date" wrote: > > "Orion Hunter" wrote in message > > However, why does this not work? > > I can answer this part. Please see "ri eval" output for more details: > > As of Ruby 1.8, local variables assigned within an eval are > only available after the eval if they were defined at the outter > scope before the eval executed. In this way eval has the same > scoping rules as blocks. > > > $>ruby -e 'str = "a = 2"; eval(str); puts a' > > -e:1: undefined local variable or method `a' for # > > (NameError) > > > > p.s. If I do the equivalent in IRB, it remembers the variable name. > > I don't know how irb manages to do it : some "binding" magic I guess. > Can experts shed some light please? > > -- shanko > Invoking "Hunt & Thomas" Programming Ruby - (Addison Wesley Longman) ... Chapter: Interactive Ruby Shell Section: Restrictions [... Excellent description ...] IMO, Ruby's behaviour conforms to PoMS in this area. (L=Least, M=Most or Maximum). - I would call it 'bonkers' :@ printf("ruby %s (%s) [%s]\n", RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM) #d='I refuse to uncomment this' eval('d=10') p d rescue eval('d+1') eval('d=12') p d rescue eval('d+1', TOPLEVEL_BINDING) p d #-> ruby 1.7.3 (2002-11-17) [i386-mswin32] #-> 11 #-> 13 #-> rb31D4.TMP:8: undefined local variable or method `d' # for # (NameError) daz