ruby 中的增量变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increment variable in ruby
提问by Carter Shaw
In Java, something like i++would increment iby 1.
在 Java 中,类似的东西i++会增加i1。
How can I do in Ruby? Surely there has to be a better way than i = i + 1?
我可以在 Ruby 中做什么?当然必须有比i = i + 1?
回答by karthikr
From the documentation,
从文档中,
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse
Ruby 没有前后递增/递减运算符。例如, x++ 或 x-- 将无法解析
So, you can do
所以,你可以做
i += 1
which is equivalent of i = i + 1
这相当于 i = i + 1

