From: Lars Christensen Date: 2008-08-29T17:10:35+09:00 Subject: Re: Please don't flame me...why is there no "++" in Ruby again ? On Aug 28, 11:43 pm, Robert Dober wrote: > On Thu, Aug 28, 2008 at 11:25 PM, Joel VanderWerf 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 objects I think the OP deserves the fuller explanation; Why are integers immutable? Consider this: a = 2 b = 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 = 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