News for hackers. Stuff that matters.
Re:Ask Matz: why the id of nil is 4? by macournoyer on Thursday February 02, 2012 @01:44PM (#3543926)
Fixnum values (numbers smaller than native one machine word minus 1 bit) are stored in an object pointer. When you do object.id it returns the address stored in that pointer.

For Ruby to know that the value in a pointer is a Fixnum (and not a pointer to an address), it will tag the first bit w/ 1 and shift the integer value by one bit. So storing the value 7 will be done like this:

  (7 << 1) + 1 # => 14
This is why 7.id == 14.

You can see where this is implemented right here: https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#...

For true, false and nil they are also stored right in the object pointer using special values defined here: https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#.... You'll notice that those 3 values in binary format all end with 0b...10. First bit to 1 means Fixnum, second bit to 1 means special value: true, false or nil.

Re:Ask Matz: why the id of nil is 4? by xentronium on Thursday February 02, 2012 @02:47PM (#3544159)
Sorry for nitpicking, but you meant 15, not 14. Other than that, it seems you are 100% right.
Re:Ask Matz: why the id of nil is 4? by macournoyer on Thursday February 02, 2012 @03:51PM (#3544396)
Oops! Indeed

  (7 << 1) + 1 # => 15
Re:Ask Matz: why the id of nil is 4? by trustfundbaby on Thursday February 02, 2012 @09:44PM (#3545486)
Might also want to use 'last' instead of 'first' in this part of your explantion

> tag the first bit w/ 1

I was confused with your explanation until I went and redid the binary on the original, then I got what you meant. sorry if its nitpicky. ignore if you got it.

Re:Ask Matz: why the id of nil is 4? by xentronium on Friday February 03, 2012 @03:04PM (#3548681)
Re:Ask Matz: why the id of nil is 4? by technomancy on Thursday February 02, 2012 @02:45PM (#3544154)
I found it very odd that he would place false before true, but then I realized that's probably just the C mindset leaking through.
Re:Ask Matz: why the id of nil is 4? by macournoyer on Thursday February 02, 2012 @03:54PM (#3544418)
Since in Ruby false and nil are the only "falsish" values and everything else is true, their values has been chosen to make a boolean test as efficient as possible. See: https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#...
Re:Ask Matz: why the id of nil is 4? by apsurd on Thursday February 02, 2012 @06:06PM (#3544954)
wow I didn't know you can multi-line highlight code like that in GitHub. Thanks for the tip!

(click on the second github link and you'll note multiple lines are highlighted. check the url for how it's done)

Re:Ask Matz: why the id of nil is 4? by silentbicycle on Thursday February 02, 2012 @03:34PM (#3544302)
You may find Gudeman's "Representing Type Information in Dynamically Typed Languages" (http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.43...) interesting - it's an overview of type tagging strategies used in many dynamically typed languages. It's not specific to Ruby (it's from 1993), but gives more context to the methods Ruby uses.
Re:Ask Matz: why the id of nil is 4? by gabrielh on Thursday February 02, 2012 @09:42PM (#3545481)
Mark Zuckerbergs facebook user id is 4 (https://graph.facebook.com/4), so its always fun when you have a nil bug and then you think Zuckerberg is using your site.
Re:Ask Matz: why the id of nil is 4? by klochner on Thursday February 02, 2012 @02:21PM (#3544077)
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.
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.

Re:Ask Matz: why the id of nil is 4? by jarin on Thursday February 02, 2012 @01:39PM (#3543900)
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.
Re:Ask Matz: why the id of nil is 4? by potomak on Thursday February 02, 2012 @12:50PM (#3543729)
Yes, you're right but, as you observed, my question is driven by curiosity.
Re:Ask Matz: why the id of nil is 4? by flgr on Friday February 03, 2012 @09:33AM (#3547187)
Hm, I think it's better style to refer to true and false as true and false (their keywords) instead of TRUE and FALSE (constants, could go away in the future?). :)

  irb(main):001:0» TRUE
  » true
  irb(main):002:0» FALSE
  » false
Re:Ask Matz: why the id of nil is 4? by JoelMcCracken on Thursday February 02, 2012 @01:23PM (#3543849)
Interesting. I had always assumed it had something to do with word size being 4.