如何在 Ruby 中增加一个整数 [英] How to increment an integer in Ruby

查看:35
本文介绍了如何在 Ruby 中增加一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What causes the error in the following code?

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

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

推荐答案

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

Ruby doesn't have an ++ operator. You can do puts 1.next though. 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 中增加一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆