Wednesday, April 18, 2007

前/后 增/减 操作符

Ruby没有前/后,增/减操作符.例如, x++ 或 x-- 会解析出错. 更重要的是, ++x 或 --x 会什么也不做! 实际上,他们表现为多个一元前缀操作符: -x == ---x == -----x == ...要增加数字,直接写 x += 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.

No comments :