Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the object_id for true and nil changed in ruby2.0?

I came across this ruby object_id allocation question sometime back and then read this awesome article which talks about VALUE and explains why object_id of true, nil and false the way they are. I have been toying with ruby2.0 object_id when I found the apparent change that has been made regarding object_id of true and nil.

forbidden:~$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
forbidden:~$
forbidden:~$ irb
irb(main):001:0> true.object_id
=> 20
irb(main):002:0> false.object_id
=> 0
irb(main):003:0> nil.object_id
=> 8
irb(main):004:0> exit
forbidden:~$
forbidden:~$ rvm use 1.9.3
Using /home/forbidden/.rvm/gems/ruby-1.9.3-p392
forbidden:~$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
forbidden:~$
forbidden:~$ irb
irb(main):001:0> true.object_id
=> 2
irb(main):002:0> false.object_id
=> 0
irb(main):003:0> nil.object_id
=> 4

tl;dr: The values for true and nil were respectively 2, 4 in 1.9.3 and 1.8.7, but have been changed to 20, 8 in ruby2.0.0 - even though the id of false remains the same i.e. 0 and the ids for Fixnum maintains the same old 2n+1 pattern.

Also, the way Fixnum and Bignum are implemented is still the same in 2.0.0 as the example given in the above mentioned article also runs just the same way it used to:

irb(main):001:0> 
irb(main):002:0* ((2**62)).class
=> Bignum
irb(main):003:0> ((2**62)-1).class
=> Fixnum
irb(main):004:0>

What's the reason behind this object_id change?

Why was this change made? How is this going to help developers?

like image 655
Bharadwaj Srigiriraju Avatar asked Mar 19 '13 17:03

Bharadwaj Srigiriraju


People also ask

Is nil an object in Ruby?

No, it is not. As I’ve already said, in Ruby every value is an object. So, nil is an object, too. In fact, it is an instance of the NilClass:

What are true false and Nil data types in Ruby?

We know that everything in Ruby is treated as an object, and so the true, false and nil as well. They are built-in types that Ruby provides to do different conditional checks and more. In this article, we will explore different examples of the true, false and nil data types and how to use them.

How to check if an object is null in Ruby?

Calling methods on a null object causes the NullPointerException to be thrown. In C, operating on invalid pointer may lead to unexpected results. As you may expect, there should be a Ruby way to check if object is nil. Here it is: # Ruby code # if my_object. nil ? puts "There is no object!"

What is the use of Boolean in Ruby?

Whenever Ruby requires a Boolean value, then nil behaves like false and values other than nil or false behave like true. In Ruby, true and false are boolean values that represent yes and no. true is an object of TrueClass and false is an object of FalseClass. Note: Ruby does not contain Boolean class.


1 Answers

A look at the Ruby source where these values are defined suggests that this has something to do with “flonums” (also see the commit where this was introduced). A search for ”flonum” came up with a message on the Ruby mailing list discussing it.

This is a technique for speeding up floating point calculations on 64 bit machines by using immediate values for some floating point vales, similar to using Fixnums for integers. The pattern for Flonums is ...xxxx xx10 (i.e. the last two bits are 10, where for fixnums the last bit is 1). The object_ids of other immediate values have been changed to accomodate this change.

You can see this change by looking at the object_ids of floats in Ruby 1.9.3 and 2.0.0.

In 1.9.3 different floats with the same value are different objects:

1.9.3p385 :001 > s = 10.234
 => 10.234 
1.9.3p385 :002 > t = 10.234
 => 10.234 
1.9.3p385 :003 > s.object_id
 => 2160496240 
1.9.3p385 :004 > t.object_id
 => 2160508080 

In 2.0.0 they are the same:

2.0.0p0 :001 > s = 10.234
 => 10.234 
2.0.0p0 :002 > t = 10.234
 => 10.234 
2.0.0p0 :003 > s.object_id
 => 82118635605473626 
2.0.0p0 :004 > t.object_id
 => 82118635605473626 
like image 190
matt Avatar answered Oct 18 '22 15:10

matt