7stud -- wrote:
> That doesn't seem to bear out.  If you switch the if statement around so 
> that the assignment happens first:
>
> def x
>     print "Function 'x' called\n"
>     99
> end
>
>   
The difference between the both examples is the following:

Ruby parses this:

  for i in 1..2
    if i == 2
      print "a=", a, "\n"
>> Here Ruby decides, that "a" must mean the a method and creates the call-method node in its tree.
    else
      a = 1
>> Here Ruby decides, a is a local variable.
      print "a=", a, "\n"
>> Here Ruby creates a evaluate-local-variable node in its tree.
    end
  end

In your example:

> for i in 1..2
>     if i == 1
>         x = 1
>   
>> Here Ruby decides, "x" is a local variable.

>         print "x=", x, "\n"
>   
>> Here Ruby creates a evaluate-local-variable node in its tree.

>     else
>         print "x=", x, "\n"
>   
 >> Sticks to its former decision, that "x" is local variable, and 
creates a evaluate-local-variable node in its tree.
>     end
> end
>   
This is an interaction between parsing and execution or ruby code, and 
it can be a bit surprising. The later execution (== comparisons, etc.) 
doesn't alter the behavior of the parser, which does its jobs earlier.

-- 
Florian Frank