Constantes Racionales en Ruby 2.1

You might be aware that floats are far from ideal when doing calculations with fixed decimal points:
irb(main):001:0> 0.1 * 3
=> 0.30000000000000004
A lot of Ruby developers fall back to using integers and only fixing it when displaying the result.

However, this only works well if you have indeed have fixed decimal points.

If not, you have to use rationals. Which isn't too bad, except there was no nice syntax for them (short of changing the return value of Integer#/).

Ruby 2.1 introduces the r suffix for decimal/rational literals to fix this:

irb(main):001:0> 0.1r
=> (1/10)
irb(main):002:0> 0.1r * 3
=> (3/10)

Casiano Rodriguez León 2015-01-07