Skip to content

Instantly share code, notes, and snippets.

@Epigene
Last active September 15, 2015 13:37
Show Gist options
  • Save Epigene/cd113d31dc28f5631219 to your computer and use it in GitHub Desktop.
Save Epigene/cd113d31dc28f5631219 to your computer and use it in GitHub Desktop.
Rails initializer for quick monkeypatches and additions to basic ruby classes
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