In an expression, when Ruby sees a name like a, it needs to decide whether a is a local variable reference or a call to method a with no parameters. Ruby uses a heuristic approach to judge this situation. When ruby resolves the source code file, it records all the symbols that have been assigned. It considers these symbols to be variables. In the future, when you encounter a symbol that can be both a variable and a method call, Ruby checks to see if the symbol has been assigned a value. If so, treat the symbol as a variable, or as a method call. Here is an example of a contrived design that describes this situation.
def a
print "Funciton ' a ' called\n" "The" I
in 1..2
if I ==2
print "a=", A, "\ n"
else
a=1
print "a=", A, "\ n" End
Output results:
A=1
Function ' a ' called
a=99
When parsing, Ruby sees the first print statement using a, and as a method call because it has not encountered any assignment statements to a. But when parsing to the second print statement, Ruby encounters an assignment statement to a, so it is treated as a variable
Note that the assignment statement is not necessarily executed-as long as Ruby sees it. The following program does not cause an error