mileszs
2/26/2009 - 8:07 PM

snippet.rb

require 'rational'

class Numeric
  def to_r(rounding = 10_000)
    Rational((self * rounding).to_i, rounding)
  end
end

class Rational
  def to_proper
    parts = divmod(1)
    parts.pop if parts.last == 0
    parts.join(' ')
  end
end

5.125.to_r.to_proper
# => 5 1/8