In Ruby 2.4.0, the Fixnum and Bignum classes were merged into Integer, resolving a long-standing issue.
Let’s see this in action.
Ruby 2.3.3
$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]
$ irb
>> 4.class
=> Fixnum
>> (2**62).class
=> Bignum
two integer classes are both subclasses of Integer.
>> 4.class.superclass
=> Integer
>> (2**62).class.superclass
=> Integer
So, in Ruby 2.3.3:
Ruby 2.4.0
Now, let’s see what occurs in Ruby 2.4.0:
$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
$ irb
>> 4.class
=> Integer
>> (2**62).class
=> Integer
No matter how small or large the number is, Ruby now uses Integer.
This change is known as “Fixnum and Bignum Unification” (Feature #12005).
Why Did Ruby Do This?
Fixnum
or a Bignum
. Everything is simply an Integer
.Fixnum
or a Bignum
directly could behave inconsistently. Now, you can rely on a single class.is_a?(Fixnum)
or is_a?(Bignum)
are no longer necessary. Instead, always check against `Integer`.Compatibility Considerations
If your code explicitly checks for Fixnum or Bignum, you’ll need to update it. For example:
Ruby 2.3.3 code
def big_number?(n)
n.is_a?(Bignum)
end
Equivalent to Ruby 2.4.0
def big_number?(n)
n.is_a?(Integer) && n > (2**62)
end
Or more simply, you might just care if it’s an integer.
n.is_a?(Integer)
Other Notable Updates in Ruby 2.4.0
Beyond integers, Ruby 2.4.0 brought some other useful improvements.
String#match?
: A faster way to test regex matches without creating a MatchData
object.
"hello".match?(/h/) # => true
Regexp#match?
: Same optimization for regex objects.Hash#transform_keys / transform_values
: Easy transformations.
{ a: 1, b: 2 }.transform_keys(&:to_s)
# => {"a"=>1, "b"=>2}
Final Thoughts
Removing Fixnum
and Bignum
might surprise developers upgrading from Ruby 2.3.x, but it’s a positive step toward making Ruby more simple and consistent.
From now on, all integers—big or small—are called Integer
.
So, the next time you run:
>> 4.class
=> Integer
Just smile and remember that Ruby’s evolution made your life a little easier.
If you’re upgrading an older Ruby application, make sure to grep your codebase for Fixnum
or Bignum
references and replace them with Integer
.
Posted on January 04, 2017 by Amit Pandya in Ruby