Skip to content

Instantly share code, notes, and snippets.

@jqr
Created February 26, 2009 15:52
Show Gist options
  • Save jqr/70921 to your computer and use it in GitHub Desktop.
Save jqr/70921 to your computer and use it in GitHub Desktop.
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.delete(0)
parts << 0 if parts.empty?
parts.join(' ')
end
end
5.125.to_r.to_proper
# => 5 1/8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment