On Thu, May 26, 2011 at 2:34 PM, Gary W. [email protected] wrote:
This seems like a simple question but it touches on some very deep issues of
Ruby’s object model. Understanding the object model is the key to understanding
why your question doesn’t make sense in Ruby.So you can see that within Ruby’s object model there is one and only one fixnum
object
that behaves like the integer 2. That particular object in MRI Ruby 1.9.2
happens to have an
object_id of 5, but that is really just an implementation detail.Lots and lots of Rubyists describe fixnum objects as ‘immutable’, but as shown
above with
instance variables, that is simply not an accurate description of Ruby’s fixnum
objects. The
characteristic that erroneously gets labeled as ‘immutability’ is that the
semantics of a fixnum object
are defined by its identity and not by its state.
That’s actually probably not the best description. While its true that
the implementation of the immutability of the arithmetic value of a
Fixnum is implemented (in most and possibly all Ruby implementations)
through a fixed relationship of that value on the identity of the
Fixnum, its worth noting that the value of a Bignum in an arithmetic
context is likewise not dependent on its state, even though Bignums –
unlike Fixnums – are generally not implemented with a fixed
relationship between value and identity, such that there can be more
than one Bignum object with the same value.
The real enforcement of the distinction isn’t so much that identity
rather than state is used (since this is not true of the
implementation of Bignums) but that you can’t define singleton methods
on objects of the built-in numeric types, and the arithmetic value
isn’t stored in mutable state (that is, its not in instance
variables), so the arithmetic behavior of objects of built-in numeric
types can’t be overridden on an object-by-object basis by the normal
methods Ruby allows for doing that for object behavior. That Fixnums
of the same value share object identity is an implementation detail,
not the fundamental basis of the arithmetic immutability, since the
latter is a feature of Ruby’s built-in numerics generally, including
those that don’t use Fixnum-style identity-sharing.