为什么 a = a 的计算结果为零?
可能的重复:
为什么 a = a 在 Ruby 中是 nil?
在幽默的 闪电演讲 - Wat 由 Gary Bernhardt 提供,以下代码的计算结果为零:
a = a
#=> nil
但是除了自赋值之外的任何内容都会导致 NameError
b = c
#=> NameError: undefined local variable or method `c' for main:Object
有人可以解释一下为什么 a = a
的计算结果为 nil
吗?
Possible Duplicate:
Why a = a is nil in Ruby?
In the humorous lightning talk - Wat delivered by Gary Bernhardt, the following code evaluates to nil:
a = a
#=> nil
But anything other than self assignment results in a NameError
b = c
#=> NameError: undefined local variable or method `c' for main:Object
Can somebody please explain why a = a
evaluates to nil
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

因为对
a
的赋值会为该范围创建a
。对b
的赋值不会创建c
。Because assignment to
a
createsa
for that scope. assignment tob
doesn't createc
.