From: Brian Candler Date: 2009-11-17T22:27:13+09:00 Subject: Re: Class inside a Method Body David A. Black wrote: > the code is executed as it's encountered, and you get output before > and after the execution of the method definition. (Of course the > method body isn't executed until there's an object to which to send > the "x" method.) Except: when reading in a source file there is an initial parsing step to build the syntax tree, and if that fails then you get a parse error and nothing is executed. Mismatched 'end' statements will do that, for example. The difference between parsing and execution is most important when considering local variables. def x puts "In method x" 99 end def meth1 x # << (A) end def meth2 x = 123 x # << (B) end def meth3 if false x = 456 end x # << (C) end puts "meth1 returns #{meth1.inspect}" puts "meth2 returns #{meth2.inspect}" puts "meth3 returns #{meth3.inspect}" The decision as to whether x is a local variable or a method call is made statically at *parse* time, before anything is executed. This is why at point (C), x is determined to a local variable: there was an assignment to x earlier in the method. However at run time that line is never actually run so its value is nil. -- Posted via http://www.ruby-forum.com/.