From: Mike Gold Date: 2009-02-08T23:46:02+09:00 Subject: Re: eval statement Albert Schlef wrote: >>Jonathan Wills wrote: >>> >>> eval('a=2') >>> puts a >>> >>> r.rb:2:in `
': undefined local variable or method `a' for >>> main:Object (NameError) >Mike Gold wrote: >> >> The contents of 'a=2' is not seen by the parser when the file is read, >> so in the second case the 'a' in 'puts a' is determined to be a method >> call: "call method 'a', then call method puts". > > I don't think you are correct here. The string 'a=2' is first *parsed*, > only then evaluated. I can't pinpoint where the misunderstanding is. The _contents_ of 'a=2' is not seen by the parser when the file is read, meaning the parser just sees a string and nothing more. The AST of the file would be the same if the string were 'foo'. Because no local assignment syntax is encountered by the parser _when the file is read_, the 'a' in 'puts a' is determined to be a method call. The parsing and evaluating of 'a=2' is done separately at runtime, long after the 'a' in 'puts a' was determined to be a method call. > If your theory was correct, eval('whatever=567') > would have generated some error (because there's no 'whatever=' method). I don't know what this theory is, but in any case 'whatever=567' would not under any circumstances be interpreted as calling method 'whatever='. The syntax x=y is always local assignment. (self.whatever=567 for the method call.) -- Posted via http://www.ruby-forum.com/.