Created
August 19, 2014 01:17
-
-
Save midwire/188dc225f6616f076184 to your computer and use it in GitHub Desktop.
Enhanced Ruby Fixnum class
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
| class Fixnum | |
| # Format a number with commas and a decimal point | |
| def commify | |
| to_s =~ /([^\.]*)(\..*)?/ | |
| int, dec = $1.reverse, $2 ? $2 : "" | |
| while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3') | |
| end | |
| int.reverse + dec | |
| end | |
| def is_even? | |
| self % 2 == 0 | |
| end | |
| def is_odd? | |
| !is_even? | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment