为什么 a = a 的计算结果为零?

发布于 12-29 09:56 字数 568 浏览 6 评论 0原文

可能的重复:
为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇2025-01-05 09:56:12

因为对 a 的赋值会为该范围创建 a。对 b 的赋值不会创建 c

Because assignment to a creates a for that scope. assignment to b doesn't create c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文