On Aug 28, 11:43=A0pm, Robert Dober <robert.do... / gmail.com> wrote:
> On Thu, Aug 28, 2008 at 11:25 PM, Joel VanderWerf<vj... / path.berkeley.edu=
> wrote:
> > John Pritchard-williams wrote:
>
> >> There's must be very good simple reason why there is no 'x++' method
> >> Ruby right?
>
> >> I did check my Ruby books by the way, but they just "unlike C there is
> >> no ++ operator in Ruby...." :)
>
> > In ruby, operators are methods and they operate on objects, not on
> > variables.
>
> .. and as Integers are immutable ++ cannot change the underlying object.
> One could define ++ if one wanted, but it can only work on mutable object=
s

I think the OP deserves the fuller explanation; Why are integers
immutable? Consider this:

a =3D 2
b =3D a
a++

Since everything is an object, 'a' references the object '2' and b
should reference it too after the assignment. Then 'a++' should
increment the object referenced by both a and b? This would of course
be possible, but it would be a rather huge performance penalty to keep
something as simple as integers as references to objects everywhere.

Ruby keeps 'small-enough' integers in the object reference (pointer)
to improve performance. Hence 'b =3D a' results in an actual copy being
taken of the value '2'. If you increment 'b', 'a' would retain its old
value, breaking the concept that a variable is always just a
reference. Ruby opts for immutable Integers instead.

Lars