0 0

Variable/Method Ambiguity5

programming ruby 2nd edition p342

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


这一段反复思量,愣是没明白。书中原文:

As Ruby parses a source file, it keeps track of symbols that have
been assigned to. It assumes that these symbols are variables. When it subsequently
comes across a symbol that could be a variable or a method call, it checks to see if
it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable;
otherwise it treats it as a method call.


我怎么觉得这段代码的运行结果和这段解释对不上?

2008年7月24日 12:07
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics