Re:Ask Matz: why the id of nil is 4?
by swatermasysk on Thursday February 02, 2012 @12:44PM
(#3543708)
Much less relevant today with Ruby 1.9.x, but still very interesting.
[ Parent
| on HN ]
Re:Ask Matz: why the id of nil is 4?
by prodigal_erik on Thursday February 02, 2012 @01:23PM
(#3543846)
Less relevant how? nil.object_id is still 4 in MRI 1.9.2.
The article noted that all integers have odd ids, but not why—the LSB is a http://c2.com/cgi/wiki?TagBit in MRI. I would hope other implementations are permitted to choose different schemes; it would suck if user code began assuming nobody found a use for more than one tag bit.
This situation was most encountered by Rails developers trying to access the ID of an ActiveRecord object. Since they changed the method to object_id in Ruby 1.9, calling id on a nil object should just give a NoMethodError instead of that slightly cryptic warning.
Re:Ask Matz: why the id of nil is 4?
by prodigal_erik on Saturday February 04, 2012 @06:06PM
(#3552539)
Ah, that was a mistake. ActiveRecord should have avoided :id until it was renamed in core Ruby, or returned some sentinel (other than nil) whose :id method fails. Assuming Ruby didn't critically depend on :id succeeding for every object, that is.