Learning Ruby... and discovering some strange things...
Hi...
In his last TechTalk Dirk showed some strange behaviours of VB.NET and C#...Dirk, have a look on this.
Look at this construction (which is by the way found by Clemens Hintze):
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
It produces:
a=1
Function 'a' called
a=99
Strange on the first hand but it gets clear when following the way Ruby disolves what is a variable and what not.
"Programming Ruby - The Pragmatic Programmer's Guide" says:
"During the parse, Ruby sees the use of a in the first print statement and, as it hasn't yet seen any assignment to a, assumes that it is a method call. By the time it gest to the second print statement though, it has seen an assignment, and so treats a as a variable."
Well, the position of the variable assignment in the text stream of the incoming script defines how the value is resolved not the actual path through the code.
Maybe I am old-fashioned but I will sign my function calls (even if there is no parameter to pass) with a() .
CU
0xff