David A. Black wrote:
> Hi --
> 
> On Sat, 7 Feb 2009, Mike Gold wrote:
> 
>>
>> The thing to remember is that local variables are always determined at
>> parse time.
>>
>> 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".
> 
> Actually it can't tell whether it's a method or a variable, so you
> get:
> 
>    undefined local variable or method `a' for main:Object (NameError)
>

No, that error message comes from rb_method_missing(), i.e., it was 
determined to be a method call.  Presumably it is worded that way for 
the user's benefit, in case there was a misspelling or some such. 
Locals are determined by the parser; method lookup is done when no such 
local exists.

> They're that way because of how they're scoped, but the assignments
> themselves are handled by eval the same way as the local ones; that
> is, there's no extra information available prior to the execution.

Locals are not handled in the same way.  The local assignment syntax is 
subject to special examination by the parser.  This does not happen for 
instance variables and globals.  Again, it is the parser that determines 
locals.  If all assignments were the same, this would print '3':

  eval("a = 3", binding)
  puts a

Pickaxe gives this example:

def a
  print "Function 'a' called\n"
  99
end

for i in 1..2
  if i == 2
    print "a=", a, "\n"
  else
    a = 1
    print "a=", a, "\n"
  end
end

produces:

a=1
Function 'a' called
a=99
-- 
Posted via http://www.ruby-forum.com/.