本文介绍了Ruby中没有增量运算符(++)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Ruby中没有增量运算符?

Why is there no increment operator in Ruby?

例如

i++
++i

++运算符是否还用于其他用途?有真实原因吗?

Is the ++ operator used for something else? Is there a real reason for this?

推荐答案

来自"Ruby新手应该知道的事情""((存档镜像)

Taken from "Things That Newcomers to Ruby Should Know" (archive, mirror)

这比以往任何时候都更好.

That explains it better than I ever could.

以及语言作者本人的原因():

and the reason from the language author himself (source):

  1. ++和-在Ruby中不是保留的运算符.
  2. C的递增/递减运算符实际上是隐藏的赋值.它们影响变量,而不影响对象.您无法通过方法完成分配. Ruby改用+ =/-=运算符.
  3. 自身不能成为分配目标.此外,更改整数1的值可能会在整个程序中引起严重的混乱.
  1. ++ and -- are NOT reserved operator in Ruby.
  2. C's increment/decrement operators are in fact hidden assignment. They affect variables, not objects. You cannot accomplish assignment via method. Ruby uses +=/-= operator instead.
  3. self cannot be a target of assignment. In addition, altering the value of integer 1 might cause severe confusion throughout the program.

这篇关于Ruby中没有增量运算符(++)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 04:02
查看更多