How to express infinity in Ruby?

Amokrane Chentir picture Amokrane Chentir · Apr 25, 2011 · Viewed 55.5k times · Source

Is there a keyword to express Infinity in Ruby?

Answer

Matt picture Matt · Apr 25, 2011

If you use ruby 1.9.2, you can use:

>> Float::INFINITY #=> Infinity
>> 3 < Float::INFINITY #=> true

Or you can create your own constant using the following*:
I've checked that in Ruby 1.8.6, 1.8.7, and 1.9.2 you have Float.infinite?.

PositiveInfinity = +1.0/0.0 
=> Infinity

NegativeInfinity = -1.0/0.0 
=> -Infinity

CompleteInfinity = NegativeInfinity..PositiveInfinity
=> -Infinity..Infinity

*I've verified this in Ruby 1.8.6 and 1.9.2