This is has been bugging me for quite some time.
>> nil.id
(irb):2: warning: Object#id will be deprecated; use Object#object_id
=> 4
Why would nil.id be 4? (or nil.object_id if you want to be picky about deprecations)
This is has been bugging me for quite some time.
>> nil.id
(irb):2: warning: Object#id will be deprecated; use Object#object_id
=> 4
Why would nil.id be 4? (or nil.object_id if you want to be picky about deprecations)
This post seems to have some information about how object_id works.
It happens because the id of every object had to somehow be represented in the C Ruby interpreter. Numbers got mapped to (number*2)+1, truthiness got 0, falseness got 2 and nil had 4 left. It´s just a weird implementation issue or leaky abstraction. You shouldn't worry about it unless you want to write a Ruby interpreter.
I'd always assumed it was a cool easter egg: In Japan 4 is the number of death.