November 18, 2016
This blog is part of our Ruby 2.4 series.
Ruby uses Fixnum
class for representing small numbers and Bignum
class for
big numbers.
# Before Ruby 2.4
1.class #=> Fixnum
(2 ** 62).class #=> Bignum
In general routine work we don't have to worry about whether the number we are
dealing with is Bignum
or Fixnum
. It's just an implementation detail.
Interestingly, Ruby also has Integer
class which is superclass for Fixnum
and Bignum
.
Starting with Ruby 2.4, Fixnum and Bignum are unified into Integer.
# Ruby 2.4
1.class #=> Integer
(2 ** 62).class #=> Integer
Starting with Ruby 2.4 usage of Fixnum and Bignum constants is deprecated.
# Ruby 2.4
>> Fixnum
(irb):6: warning: constant ::Fixnum is deprecated
=> Integer
>> Bignum
(irb):7: warning: constant ::Bignum is deprecated
=> Integer
We don't have to worry about this change most of the times in our application code. But libraries like Rails use the class of numbers for taking certain decisions. These libraries need to support both Ruby 2.4 and previous versions of Ruby.
Easiest way to know whether the Ruby version is using integer unification or not is to check class of 1.
# Ruby 2.4
1.class #=> Integer
# Before Ruby 2.4
1.class #=> Fixnum
Look at PR #25056 to see how Rails is handling this case.
Similarly Arel is also supporting both Ruby 2.4 and previous versions of Ruby.
If this blog was helpful, check out our full blog archive.