BigBinary Blog

We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.

Ruby 2.4 unifies Fixnum and Bignum into Integer

This blog is part of our Ruby 2.4 series.

Ruby uses Fixnum class for representing small numbers and Bignum class for big numbers.

1# Before Ruby 2.4
31.class         #=> Fixnum
4(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.

1# Ruby 2.4
31.class         #=> Integer
4(2 ** 62).class #=> Integer

Starting with Ruby 2.4 usage of Fixnum and Bignum constants is deprecated.

1# Ruby 2.4
3>> Fixnum
4(irb):6: warning: constant ::Fixnum is deprecated
5=> Integer
7>> Bignum
8(irb):7: warning: constant ::Bignum is deprecated
9=> Integer

How to know if a number is Fixnum, Bignum or 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.

1# Ruby 2.4
31.class #=> Integer
5# Before Ruby 2.4
61.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.

Prathamesh Sonpatki in Ruby 2.4
November 18, 2016

Subscribe to our newsletter