[FAQ] Why isn't 'x++' or 'x--' valid?

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? :wink:

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) end

Beware 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

Hi,

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).

I guess you’re captured by your languages too much. Neither C nor
English is universal. I don’t feel ++/-- fit in “Ruby Way”, just
because it cannot be explained by simple object model. Illusion from
sugaring may confuse people afterwards.

And I took the little book out of the angel’s hand, and ate it up;
and it was in my mouth sweet as honey: and as soon as I had eaten
it, my belly was bitter.
– Rev 10:10

						matz.
···

In message “Re: [FAQ] Why isn’t ‘x++’ or ‘x–’ valid?” on 02/11/26, “Peña, Botp” botp@delmonte-phil.com writes:

Peña, Botp wrote:

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.

I seriously doubt that there are C/C++/Java programmers holding back
from Ruby just for the want of a pre/post increment/decrement operator.

If they can’t see Ruby for what is then from experience I know that they
will be asking for strong typing, statics, operator overloading,
constant methods and parameter and a whole host of other features that
will just turn Ruby into a second rate C/C++/Java.

Quite frankly the only thing that I miss in Ruby is an String#each_char
to complement String#each_byte.

In article 20021126153521.784921BC283@proxy2-dmz.delmonte-phil.com,

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

Surely you can just modify this by using a editor to convert ++x to
x+=1. Using a grep editor you could do the lot at once.

Personally I have hated the ++x and x++ idiom from the day I first saw
them in C and I consider their omission from Ruby to be one of common
sense.

I consider the aesthetics of a language is defined by not what you put
into it but what you leave out. Generally Matz has got it spot on.

Ross

···

“Peña, Botp” botp@delmonte-phil.com wrote: