Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Feb 21 ’11

How to define infinite numbers

You can define the “number” ‘infinity’ (or ‘-infinity’) in Ruby like this:

1.0/0
=> Infinity

-1.0/0
=> -Infinity

Infinity and -Infinity behave just like you expect them to: they are always bigger - or always smaller, respectively - than any number you compare it to.
The Xing Devblog had a great post recently about using Ruby’s infinity in Rails, with some thoughts on when you might want to use it (or not).

P.S.: the Float class in Ruby 1.9.2 provides an Infinity constant you can use:

Float::INFINITY
=> Infinity

This tip was submitted by makoto.

9 notes 0 comments

  1. makoto submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus