shreeve / s2s.org (Steve Shreeve) writes:

> Am I doing something wrong? If "counter += 1" works, shouldn't "counter++"?

Ruby doesn't have a ++ operator.

The reason is interesting (I think). Most Ruby operators are
interpreted as messages, sent to a receiver.

  counter = counter + 1

sends the message '+' to the object referenced by the variable
'counter', passing it the argument '1'. Thi object then returns
something appropriate (the next integer is counter is a Fixnum), and
the resulting (new) object is assigned to counter.

  counter += 1

is effectively shorthand for this process.

However, counter++ is different. This would be telling counter to
increment itself. But variables don't understand messages, objects
do.

    __________            __ Fixnum __
   | counter  | -------->|  1234      |
   |__________|          |____________|

    counter++

Now, what should happen here? There's no assignment, so counter will
still reference the same object afterwards. So for it to work, you'd
have to change the value of '1234'. This would be unfortunate, as
Ruby's semantics act as if there's only one '1234' object in the
system, shared among all variables that hold that value.
Languages such as C++ and Java don't have this problem, as basic types 
such as Fixnum are not objects.

Regards


Dave


Footnotes: 
 The implementation is actually different, but that's another story.