This is from the example given in ruby-doc section 4.3, in the following link:
The explanation goes like this:
Ruby looks for assignment statements. If at some point in the source prior to the use of
a'' it sees it being assigned to, it decides to parse
a'' as a variable, otherwise it treats it as a method.
the example is below
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
The output is given as
a=1
Function 'a' called
a=99
But as evident from the code, when i is 1, a is assigned to 1 an 1 is printed as value of a. Then for i = 2, method 'a' is called.
Now what will happen if I print 'a' outside the for loop? I got the value 1, but I have no clue how that is possible. If reassigning a to 1 from the previous value of method is going to change it everywhere, then during i = 2 also the output should've been 1. Correct me if I'm wrong.