Created
August 13, 2016 10:34
-
-
Save yemster/0e27e1072b75b990cb15bd3fe9661e4f to your computer and use it in GitHub Desktop.
Dedecimalised whole numbers in ruby - This extends and overrides ruby core `BigDecimal#to_s`.
Caution: Don't do this - bad things begin to happen very quickly in other gems and third part addons
PS: If must be used, use with extreme caution as this behaviour will likely cause issues and incompatibility with other libraries.
This file contains 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
# Extends Ruby BigDecimal class so we have an updated to_s method that returns numbers witheout the ZERO at the end | |
# if the number is an integer. | |
# e,g. '12.0'.to_s => 12 | |
# '9.23'.to_s => 9.23 | |
class BigDecimal | |
alias_method(:original_to_s, :to_s) unless method_defined?(:original_to_s) | |
def is_whole_number? | |
self % 1 == 0 | |
end | |
def to_s(*args) | |
if is_whole_number? | |
to_i.to_s | |
else | |
original_to_s(*args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment