Why there are always fixed object_id s for Boolean objects?

If you find the below test,you would see that, object_ids are always
same for the three objects - true,false and nil.

Why for these objects object_id s are not allocated in run time?

true.object_id
=> 2

false.object_id
=> 0

nil.object_id
=> 4

‘a’.object_id
=> 17515344

nil.object_id
=> 4

‘a’.object_id
=> 18433428

false.object_id
=> 0

‘a’.object_id
=> 18454176

true.object_id
=> 2

Because they refers to constants created when the VM arise, always.

nil.==(NIL)
true.==(TRUE)
false.==(FALSE)

Perhaps could be a better explanation.

As usual

http://ruby-doc.org/core-2.0/TrueClass.html

John

object_ids are also the same for Fixnum objects

its a feature not a bug

PS: but they can differ between different ruby versions

(in 2.0 little floats are also have fixed ids)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs