Hi sir Gavin Sinclair [mailto:gsinclair@soyabean.com.au]:
You wrote:
[snip]
At best, x++ could be syntax sugar for ‘x = x + 1’, but for
whatever reason - be it a diminished appetite for sugar, or
perhaps no intention to mislead - the creator of Ruby did not
include these operators/methods.
Matz indeed suggested long way back that this would be possible but only as
a syntax sugar (maybe preprocess it to .succ/.next/+=/-=). Considering a lot
of “surprises” lately, I would vote for this.
The ++/-- has become a universal idiom for a lot of programmers, just like
“fck yu” idiom has been known worldwide (heck even aborigines say it -pls
excuse my language, this is just stupid example).
In fact, this would be a big plus for ruby, too. We could say then to our
c++/java friends that their ++ is welcome here, though it would just be
icing on the cake; ruby’s oo is the meat.
Anyway, what do we lose. It’s just sugar/icing, or salt?
Because Ruby has such good support for iterators, there is
actually very little need for explicit increment/decrement
operators. The following statements are the Ruby equivalents
of plain ‘for loops’ and array iteration (the most common use
of x++ in other languages):10.times do action end
array.each do |x| action(x) endBeware that these are not actually errors in Ruby. They will
be parsed according to their context, so
x++ # incomplete statement (x + +what?)
array[x++] # parse error
++x # +(+x) == x
–x # -(-x) == x
array[++x] # → array
Currently, I am converting a perl program to ruby and it pains me since it
has a lot of the form:
if cond1
++x
++y
++z
end
if cond2
++a
++b
++c
++x
end
see http://jimsun.linxnet.com/downloads/pflogsumm-1.0.3.pl for the original.
Gavin
Many thanks,
-botp