如何在 Ruby 中增加一个整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7993915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:26:42  来源:igfitidea点击:

How to increment an integer in Ruby

ruby

提问by robert

What causes the error in the following code?

是什么导致了以下代码中的错误?

ruby -e "puts 1++"
-e:1: syntax error, unexpected $end

or

或者

ruby -e "x=1; puts x++;"
-e:1: syntax error, unexpected ';'

回答by Michael Kohl

Ruby doesn't have an ++operator. You can do puts 1.nextthough. Note that for your second example this would not change the value of x, in that case you'd have to use x += 1.

Ruby 没有++运算符。你可以这样做puts 1.next。请注意,对于您的第二个示例,这不会更改 的值x,在这种情况下,您必须使用x += 1.