Created
June 19, 2013 05:10
-
-
Save RohitRox/5811813 to your computer and use it in GitHub Desktop.
useful methods additions for ruby core classes
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
class Numeric | |
def roundup(nearest=10) | |
self % nearest == 0 ? self : self + nearest - (self % nearest) | |
end | |
def rounddown(nearest=10) | |
self % nearest == 0 ? self : self - (self % nearest) | |
end | |
end | |
puts 2.roundup #=> 10 | |
puts 23.roundup #=> 30 | |
puts 20.roundup #=> 20 | |
puts 45.roundup #=> 50 | |
puts 156.roundup #=> 160 | |
puts 156.34.roundup #=> 160.0 | |
puts 16.34.roundup #=> 20.0 | |
puts 81.1234.roundup #=> 90.0 | |
class String | |
def to_range | |
self.split('..').inject { |s,e| s.to_i..e.to_i } | |
end | |
end | |
"0..230".to_range | |
#=> 0..230 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment