How to express infinity in Ruby?

RubyInfinity

Ruby Problem Overview


Is there a keyword to express Infinity in Ruby?

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

No keyword, but 1.9.2 has a constant for this:

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

Solution 3 - Ruby

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html#label-Infinity

1.9.3p429 :025 > BigDecimal('Infinity')
 => #<BigDecimal:7f8a6c548140,'Infinity',9(9)>
1.9.3p429 :026 > BigDecimal('-Infinity')
 => #<BigDecimal:7f8a6a0e3728,'-Infinity',9(9)>
1.9.3p429 :027 > 3 < BigDecimal('Infinity')
 => true

1.9.3p429 :028 > BigDecimal::INFINITY
 => #<BigDecimal:7f8a6ad046d8,'Infinity',9(9)>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmokrane ChentirView Question on Stackoverflow
Solution 1 - RubyMattView Answer on Stackoverflow
Solution 2 - RubyMichael KohlView Answer on Stackoverflow
Solution 3 - RubydavidtingsuView Answer on Stackoverflow