Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_extensions.rb

Instance Method Summary collapse

Instance Method Details

#even?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/dragonfly_extensions.rb', line 113

def even?
  self & 1 == 0
end

#factorialObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dragonfly_extensions.rb', line 96

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/dragonfly_extensions.rb', line 109

def odd?
  self & 1 != 0
end

#rounddown(nearest = 10) ⇒ Object



82
83
84
# File 'lib/dragonfly_extensions.rb', line 82

def rounddown(nearest=10)
  self % nearest == 0 ? self : self - (self % nearest)
end

#roundnearest(nearest = 10) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/dragonfly_extensions.rb', line 86

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



78
79
80
# File 'lib/dragonfly_extensions.rb', line 78

def roundup(nearest=10)
  self % nearest == 0 ? self : self + nearest - (self % nearest)
end

#to_years_and_monthsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dragonfly_extensions.rb', line 59

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