I'm playing around with Ruby's .object_id and noticed that, in several sequential sessions of irb, I get these identical results:

false.object_id // 0
true.object_id // 2
nil.object_id // 4
100.object_id // 201

In fact, every integer's object_id seems to be ((value * 2) + 1).

On the other hand, a given string's object_id is never the same after exiting and re-running irb.

This raises several questions for me:

  1. Is there a known scheme by which certain object_ids are determined? Are others basically random?
  2. The ids for true, false, and nil, aren't sequential. Is there a way to ask what object is represented by a given id? (I'm curious what the other single-digit and ids are tied to.)
  3. Could you (not that you should) write obfuscated Ruby where you use known object ids to refer to objects without naming them, like "object of id 201 + object of id 19" to mean "100 + 9"?

Update

Using Andrew Grimm's suggestion, I tried discovering other "low id" objects, but found that:

  • There don't appear to be any more even-numbered objects in this sequence - ids 6, 8, 10, etc don't point to anything.
  • As implied by my earlier experiment, all the odd-numbered ids belong to numbers. Specifically, id 1 points to the number 0, 3 points to 1, 5 points to 2, and so forth.
Edit
Report