Last active
September 15, 2015 13:37
-
-
Save Epigene/cd113d31dc28f5631219 to your computer and use it in GitHub Desktop.
Rails initializer for quick monkeypatches and additions to basic ruby classes
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 Numeric | |
# 200.01.safe_percent(50) #=> 25.% | |
def safe_percent(part) | |
if self > 0 | |
if part >= 0 | |
return 100*part.to_f/self.to_f | |
else | |
raise "Sorry, no negative numbers please" | |
end | |
elsif self == 0 | |
if part == 0 | |
return 0 | |
else | |
raise "You are trying to do #{part}/#{self}*100%, WTF?" | |
end | |
else | |
raise "Sorry, no negative numbers please" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment