Skip to content

Instantly share code, notes, and snippets.

@RohitRox
Created June 19, 2013 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RohitRox/5811813 to your computer and use it in GitHub Desktop.
Save RohitRox/5811813 to your computer and use it in GitHub Desktop.
useful methods additions for ruby core classes
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