Created
February 26, 2009 15:52
-
-
Save jqr/70921 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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