Wednesday 3 October 2012

Called id for nil


 "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"
As a Ruby on Rails developer I am very much familiar with the above kind of error and often i used to bang my head as to why this.... and at last am able to find the reason behind it.

When Ruby interpreter boots up it initializes FalseClass, TrueClass and NilClass. As we know that false, true variable work 
exactly the same way as nil does.They are singleton instances of 
FalseClass and TrueClass,respectively.


false.object_id
 => 0

true.object_id
 => 2

nil.object_id
 => 4

What happened to 1 and 3? Well, the first bit is reserved for Fixnum values (numbers) only


No comments:

Post a Comment