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

1#infinite?
2
35.0.infinite?
4=> nil
5
6Float::INFINITY.infinite?
7=> 1
8
95.infinite?
10NoMethodError: undefined method `infinite?' for 5:Fixnum
11
1#finite?
2
35.0.finite?
4=> true
5
65.finite?
7NoMethodError: undefined method `finite?' for 5:Fixnum
8

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.

1#infinite?
2
35.0.infinite?
4=> nil
5
6Float::INFINITY.infinite?
7=> 1
8
95.infinite?
10=> nil
11
1#finite?
2
35.0.finite?
4=> true
5
65.finite?
7=> true
8

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.