globewalldesk globewalldesk -4 years ago 106
Ruby Question

Why do Ruby object_ids for true, false, and nil seem to differ in Windows vs. Ubuntu?

In my Windows 10 system running Ruby 2.3.3, irb told me:

=> 0
=> 2
=> 4

But then on, in Ubuntu 14.04.3, running Ruby 2.3.0, irb told me:

=> 0
=> 20
=> 8

Not that this matters (who knows?), but why are these numbers different?

Note: this is not quite the same question as this one. They're both Ruby 2.3.

Answer Source

As the accepted answer on the question you linked to says, 0, 20, and 8 are the Object IDs that are used when using flonums on YARV.

So, obviously, your Windows implementation is not using flonums. The most likely reason is that either you are not using YARV (e.g. using JRuby or Rubinius), or you are using YARV, but a 32 bit version.

Note: Object IDs are a private internal implementation detail of the particular Ruby implementation. The version of Ruby you are using is completely irrelevant. What is relevant is which implementation you are using, which version, which commandline options, how you compiled it, what your environment is and so on. It has nothing to do with the language.

Note also: you should never ever rely on the specific value of Object IDs. Object IDs guarantee two things, and only these two things:

  1. An object does not change its Object ID during its lifetime.
  2. No two objects have the same Object ID at the same time.

Here are some things that are not guaranteed:

  • It is not guaranteed that an object will have the same Object ID during different runs of the program.
  • It is not guaranteed that an Object ID is unique during the runtime of the program, it may be re-used for different objects as long as those objects don't live at the same time.
  • It is not guaranteed that the Object ID will follow a certain pattern, e.g. that it is always the memory address of the object (not true on JRuby, for example) or that it is always a specific value (not true for nil and false, as you have just discovered), or that it is always related to the object's value in some way (due to the specific way that YARV optimizes Integers, the Object ID for small integers will always be 2n+1, but that is only true for small integers (where the definition of "small" depends on whether you run a 64 bit or 32 bit version of YARV) and it is only an implementation detail that may change at any moment without notice).
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download