In Ruby, you can use ObjectSpace._id2ref(n)
to return a reference to an object with object_id n. For example:
$ a = "foo"
$ a.object_id
70142658775260
$ ObjectSpace._id2ref(a.object_id)
"foo"
I was exploring this and wanted to know what object had object_id 4
, but ObjectSpace._id2ref(4)
raises the following RangeError
:
RangeError: 0x00000000000004 is not id value
My understanding is that before Ruby 2.0, nil
's object id was fixed at 4, but in Ruby 2.0 nil
has object id 8. There's an explanation for this here.
Is the error raised simply because no object has 4 as a fixed object id? If so, is it possible for some object foo
to happen to be assigned object_id
4 at runtime?