Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- Defined in:
- lib/dragonfly_extensions/monkey_patches.rb
Instance Method Summary collapse
- #even? ⇒ Boolean
- #factorial ⇒ Object
- #odd? ⇒ Boolean
- #rounddown(nearest = 10) ⇒ Object
- #roundnearest(nearest = 10) ⇒ Object
- #roundup(nearest = 10) ⇒ Object
- #to_years_and_months ⇒ Object
Instance Method Details
#even? ⇒ Boolean
101 102 103 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 101 def even? self & 1 == 0 end |
#factorial ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 84 def factorial my_i = self.to_i if my_i <= 0 result = 0 else result = 1 my_i.downto(2) do |n| result = result * n end end result end |
#odd? ⇒ Boolean
97 98 99 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 97 def odd? self & 1 != 0 end |
#rounddown(nearest = 10) ⇒ Object
70 71 72 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 70 def rounddown(nearest=10) self % nearest == 0 ? self : self - (self % nearest) end |
#roundnearest(nearest = 10) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 74 def roundnearest(nearest=10) up = roundup(nearest) down = rounddown(nearest) if (up-self) < (self-down) return up else return down end end |
#roundup(nearest = 10) ⇒ Object
66 67 68 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 66 def roundup(nearest=10) self % nearest == 0 ? self : self + nearest - (self % nearest) end |
#to_years_and_months ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 47 def to_years_and_months case when self == 0 "0 months" when self < 12 "#{ self} month#{ self == 1 ? '' : 's' }" when self == 12 "1 year" when self > 12 years = self / 12 months = self % 12 if (months > 0) "#{years} year#{ years > 1 ? 's' : '' } and #{ months} month#{ months == 1 ? '' : 's' }" else "#{years} year#{ years > 1 ? 's' : '' }" end end end |