Ruby 中没有增量运算符 (++)?
Possible Duplicate:
Why doesn't Ruby support i++ or i— for fixnum?
Why is there no increment operator in Ruby?
e.g.
i++
++i
Is the ++
operator used for something else? Is there a real reason for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
摘自《Ruby 新手应该知道的事情》
" (存档,mirror)
这比我能更好地解释它。
编辑:和来自语言作者本人的原因(
Taken from "Things That Newcomers to Ruby Should Know
" (archive, mirror)
That explains it better than I ever could.
EDIT: and the reason from the language author himself (source):
来自 Matz 的帖子:
From a posting by Matz:
我认为这种表示法不可用,因为与 PHP 或 C 不同,Ruby 中的一切都是对象。
当然你可以使用
$var=0; PHP 中的 $var++
,但那是因为它是一个变量而不是一个对象。因此,$var = new stdClass(); $var++ 可能会抛出错误。我不是 Ruby 或 RoR 程序员,所以我确信有人可以验证上述内容或纠正它(如果不准确)。
I don't think that notation is available because—unlike say PHP or C—everything in Ruby is an object.
Sure you could use
$var=0; $var++
in PHP, but that's because it's a variable and not an object. Therefore,$var = new stdClass(); $var++
would probably throw an error.I'm not a Ruby or RoR programmer, so I'm sure someone can verify the above or rectify it if it's inaccurate.