Following code was tested with ruby 1.8.7 and Rails 2.3 .
While developing rails application you have must seen this
We all know that this message is added by Rails and it is called
whiny nil .
If you open your
config/development.rb file you will see
Simply stated it means that if the application happens to invoke id on a nil object then throw an error. Rails assumes that under no circumstance a developer wants to find id of a nil object. So this must be an error case and Rails throws an exception.
The question I have is why 4. Why Matz chose the id of nil to be 4. This awesome presentation on ‘Ruby Internals’ has the answer.
In short Matz decided to have all the odd numbers reserved for numerical values. Check this out.
Id 1,3,5 and 7 are taken by 0,1,2 and 3.
Now we are left with the id 0,2,4 and higher values.
FALSE had the id 0 and TRUE has the id 2.
Now the next available id left is 4 and that is taken by NIL.
We won’t even be discussing this issue once 1.9 comes out
where we will have to use
object_id and then this won’t be an issue.
You can follow more discussion about this article at Hacker news .