Ruby 2.4 adds infinite? and finite? methods to Numeric

Abhishek Jain

Abhishek Jain

December 19, 2016

This blog is part of our  Ruby 2.4 series.

Prior to Ruby 2.4

Prior to Ruby 2.4, Float and BigDecimal responded to methods infinite? and finite?, whereas Fixnum and Bignum did not.

Ruby 2.3

#infinite?

5.0.infinite?
=> nil

Float::INFINITY.infinite?
=> 1

5.infinite?
NoMethodError: undefined method `infinite?' for 5:Fixnum

#finite?

5.0.finite?
=> true

5.finite?
NoMethodError: undefined method `finite?' for 5:Fixnum

Ruby 2.4

To make behavior for all the numeric values to be consistent, infinite? and finite? were added to Fixnum and Bignum even though they would always return nil.

This gives us ability to call these methods irrespective of whether they are simple numbers or floating numbers.

#infinite?

5.0.infinite?
=> nil

Float::INFINITY.infinite?
=> 1

5.infinite?
=> nil

#finite?

5.0.finite?
=> true

5.finite?
=> true

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.