On Tue, Feb 24, 2009 at 4:12 PM, David M. [email protected]
b = 2 # or any other object! a still points to 1 while b points to 2
I only pointed it out because there’s no way (that I know of) to show
that a and b point to the same object at one point if a Fixnum is
used. You can use ‘hi’.sub!, but there’s no 1.succ!
As Robert K. pointed out, you can tell whether two variables
the same object using object_id
Two references a, and b refer to the same object iff a.object_id ==
This is true regardless of the class of the object(s) involved in the
Object#equal? should also work. Object#== checks object identity by
default, but is often overridden to implement a more ‘value based’
By convention classes are not supposed to override Object#equal?
However, Fixnums can have instance variables, just like any other
Yes, but I’m not sure what that has to do with determining whether a fixnum
is identical to another object.
By the way, instance variables for immediate objects like fixnums are
implemented differently than instance variables of objects which take up
For non-immediate objects, the instance data has a pointer to a hash
which maps the iv name to a value (in Ruby < 1.9) or to shared hash
which maps the iv name to an index.
For immediate objects, there’s a global hash which maps the object_id of
object to a hash containing that instance’s instance variable names and
One thing that immediate objects CAN’T have is a singleton class and
therefore singleton methods.
irb(main):001:0> a = “a String”
=> “a String”
irb(main):002:0> def a.foo;“foo”;end
irb(main):004:0> a = 1
irb(main):005:0> def a.foo;“bah”;end
TypeError: can’t define singleton method “foo” for Fixnum
This is because immediate objects don’t have a place for the klass
which points to the first module, or module like object searched for
methods, so there’s no place to insert a singleton class at the head of
chain for immediate objects. In the case of immediate objects, the Ruby
knows from the bit encoding of the value that it’s a FixNum, or nil, or
…, and starts the search with the particular class.