Class: Numeric
Instance Method Summary collapse
- #add(num) ⇒ Object
-
#clamp(minimum, maximum = nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#decrement(amount = 1.0) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #distance(num) ⇒ Object
- #divide(num) ⇒ Object
- #equal_to?(num) ⇒ Boolean (also: #eq?)
- #fraction ⇒ Object
- #fraction? ⇒ Boolean
- #greater_than?(num) ⇒ Boolean (also: #gt?)
- #greater_than_or_equal_to?(num) ⇒ Boolean (also: #gteq?)
- #increment(amount = 1.0) ⇒ Object
- #inside?(start, finish) ⇒ Boolean
- #less_than?(num) ⇒ Boolean (also: #lt?)
- #less_than_or_equal_to?(num) ⇒ Boolean (also: #lteq?)
- #multiple_of?(number) ⇒ Boolean
- #multiply(num) ⇒ Object
- #not_equal_to?(num) ⇒ Boolean (also: #not_eq?, #inequal_to?, #ineq?)
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #outside?(start, finish) ⇒ Boolean
- #pad(options = {}) ⇒ Object
-
#pad_precision(options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#percentage_of(number) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity.
- #power(num) ⇒ Object
- #root(num) ⇒ Object
- #subtract(num) ⇒ Object
- #to_currency(options = {}) ⇒ Object
- #to_nearest_value(values = []) ⇒ Object
- #to_percentage(options = {}) ⇒ Object
- #within?(number, epsilon = 0.01) ⇒ Boolean
Instance Method Details
#add(num) ⇒ Object
5 6 7 |
# File 'lib/lite/ruby/numeric.rb', line 5 def add(num) self + num end |
#clamp(minimum, maximum = nil) ⇒ Object
rubocop:disable Metrics/MethodLength
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lite/ruby/numeric.rb', line 10 def clamp(minimum, maximum = nil) if minimum.is_a?(Range) maximum = minimum.max minimum = minimum.min end if minimum > self minimum elsif maximum < self maximum else self end end |
#decrement(amount = 1.0) ⇒ Object
rubocop:enable Metrics/MethodLength
26 27 28 |
# File 'lib/lite/ruby/numeric.rb', line 26 def decrement(amount = 1.0) self - amount end |
#distance(num) ⇒ Object
30 31 32 |
# File 'lib/lite/ruby/numeric.rb', line 30 def distance(num) (self - num).abs end |
#divide(num) ⇒ Object
34 35 36 37 38 |
# File 'lib/lite/ruby/numeric.rb', line 34 def divide(num) return num if num.zero? self / num end |
#equal_to?(num) ⇒ Boolean Also known as: eq?
40 41 42 |
# File 'lib/lite/ruby/numeric.rb', line 40 def equal_to?(num) self == num end |
#fraction ⇒ Object
46 47 48 |
# File 'lib/lite/ruby/numeric.rb', line 46 def fraction (self - truncate).abs end |
#fraction? ⇒ Boolean
50 51 52 |
# File 'lib/lite/ruby/numeric.rb', line 50 def fraction? fraction != 0.0 end |
#greater_than?(num) ⇒ Boolean Also known as: gt?
54 55 56 |
# File 'lib/lite/ruby/numeric.rb', line 54 def greater_than?(num) num < self end |
#greater_than_or_equal_to?(num) ⇒ Boolean Also known as: gteq?
60 61 62 |
# File 'lib/lite/ruby/numeric.rb', line 60 def greater_than_or_equal_to?(num) num <= self end |
#increment(amount = 1.0) ⇒ Object
66 67 68 |
# File 'lib/lite/ruby/numeric.rb', line 66 def increment(amount = 1.0) self + amount end |
#inside?(start, finish) ⇒ Boolean
70 71 72 |
# File 'lib/lite/ruby/numeric.rb', line 70 def inside?(start, finish) (start < self) && (finish > self) end |
#less_than?(num) ⇒ Boolean Also known as: lt?
74 75 76 |
# File 'lib/lite/ruby/numeric.rb', line 74 def less_than?(num) num > self end |
#less_than_or_equal_to?(num) ⇒ Boolean Also known as: lteq?
80 81 82 |
# File 'lib/lite/ruby/numeric.rb', line 80 def less_than_or_equal_to?(num) num >= self end |
#multiple_of?(number) ⇒ Boolean
90 91 92 93 94 |
# File 'lib/lite/ruby/numeric.rb', line 90 def multiple_of?(number) return zero? if number.zero? modulo(number).zero? end |
#multiply(num) ⇒ Object
86 87 88 |
# File 'lib/lite/ruby/numeric.rb', line 86 def multiply(num) self * num end |
#not_equal_to?(num) ⇒ Boolean Also known as: not_eq?, inequal_to?, ineq?
96 97 98 |
# File 'lib/lite/ruby/numeric.rb', line 96 def not_equal_to?(num) self != num end |
#ordinal ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lite/ruby/numeric.rb', line 104 def ordinal return 'th' if (11..13).cover?(abs % 100) case abs % 10 when 1 then 'st' when 2 then 'nd' when 3 then 'rd' else 'th' end end |
#ordinalize ⇒ Object
115 116 117 |
# File 'lib/lite/ruby/numeric.rb', line 115 def ordinalize "#{self}#{ordinal}" end |
#outside?(start, finish) ⇒ Boolean
119 120 121 |
# File 'lib/lite/ruby/numeric.rb', line 119 def outside?(start, finish) (start > self) || (finish < self) end |
#pad(options = {}) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/lite/ruby/numeric.rb', line 123 def pad( = {}) pad_number = [:pad_number] || 0 precision = [:precision] || 3 to_s.rjust(precision, pad_number.to_s) end |
#pad_precision(options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/lite/ruby/numeric.rb', line 132 def pad_precision( = {}) pad_number = [:pad_number] || 0 precision = [:precision] || 2 separator = [:separator] || '.' string = to_s string << separator unless string.include?(separator) ljust_count = string.split(separator).first.length ljust_count += (string.count(separator) + precision) if precision.positive? if ljust_count >= string.length string.ljust(ljust_count, pad_number.to_s) else string[0..(ljust_count - 1)] end end |
#percentage_of(number) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
151 152 153 154 155 |
# File 'lib/lite/ruby/numeric.rb', line 151 def percentage_of(number) return 0 if zero? || number.zero? (self / number.to_f) * 100.0 end |
#power(num) ⇒ Object
157 158 159 |
# File 'lib/lite/ruby/numeric.rb', line 157 def power(num) self**num end |
#root(num) ⇒ Object
161 162 163 |
# File 'lib/lite/ruby/numeric.rb', line 161 def root(num) self**(1.0 / num) end |
#subtract(num) ⇒ Object
165 166 167 |
# File 'lib/lite/ruby/numeric.rb', line 165 def subtract(num) self - num end |
#to_currency(options = {}) ⇒ Object
169 170 171 172 173 |
# File 'lib/lite/ruby/numeric.rb', line 169 def to_currency( = {}) unit = [:unit] || '$' "#{unit}#{pad_precision(.only(:precision))}" end |
#to_nearest_value(values = []) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/lite/ruby/numeric.rb', line 175 def to_nearest_value(values = []) return self if values.length.zero? value = values.first difference = (self - value).abs values.each do |val| next unless (self - val).abs < difference difference = (self - val).abs value = val end value end |
#to_percentage(options = {}) ⇒ Object
191 192 193 194 195 |
# File 'lib/lite/ruby/numeric.rb', line 191 def to_percentage( = {}) unit = [:unit] || '%' "#{pad_precision(.only(:precision))}#{unit}" end |
#within?(number, epsilon = 0.01) ⇒ Boolean
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/lite/ruby/numeric.rb', line 197 def within?(number, epsilon = 0.01) return number == self if epsilon.zero? alpha = to_f beta = number.to_f if alpha.zero? || beta.zero? (alpha - beta).abs < epsilon else (alpha / beta - 1).abs < epsilon end end |