Class: Numeric
Instance Method Summary collapse
- #add(num) ⇒ Object
- #at_least(lower) ⇒ Object
- #at_most(upper) ⇒ Object
-
#clamp(minimum, maximum = nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#close?(number, epsilon = 0.01) ⇒ Boolean
rubocop:enable Metrics/MethodLength.
- #decrement(amount = 1.0) ⇒ Object
- #delimit(options = {}) ⇒ Object
- #delta(num) ⇒ Object
- #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?)
- #markdown_percentage(percent) ⇒ Object
- #markup_percentage(percent) ⇒ Object
- #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
- #range(value) ⇒ Object (also: #plus_minus)
- #root(num) ⇒ Object
- #round_down(num = 0) ⇒ 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
6 7 8 |
# File 'lib/lite/ruby/numeric.rb', line 6 def add(num) self + num end |
#at_least(lower) ⇒ Object
10 11 12 |
# File 'lib/lite/ruby/numeric.rb', line 10 def at_least(lower) self >= lower ? self : lower end |
#at_most(upper) ⇒ Object
14 15 16 |
# File 'lib/lite/ruby/numeric.rb', line 14 def at_most(upper) self <= upper ? self : upper end |
#clamp(minimum, maximum = nil) ⇒ Object
rubocop:disable Metrics/MethodLength
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lite/ruby/numeric.rb', line 19 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 |
#close?(number, epsilon = 0.01) ⇒ Boolean
rubocop:enable Metrics/MethodLength
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lite/ruby/numeric.rb', line 35 def close?(number, epsilon = 0.01) return self == number if epsilon.zero? a = to_f b = number.to_f if a.zero? || b.zero? (a - b).abs < epsilon else (a / b - 1).abs < epsilon end end |
#decrement(amount = 1.0) ⇒ Object
48 49 50 |
# File 'lib/lite/ruby/numeric.rb', line 48 def decrement(amount = 1.0) self - amount end |
#delimit(options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lite/ruby/numeric.rb', line 52 def delimit( = {}) delimiter = [:delimiter] || ',' separator = [:separator] || '.' digits, decimals = to_s.split('.') digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse return digits unless decimals [digits, decimals].join(separator) end |
#delta(num) ⇒ Object
64 65 66 |
# File 'lib/lite/ruby/numeric.rb', line 64 def delta(num) (self - num).abs end |
#distance(num) ⇒ Object
68 69 70 |
# File 'lib/lite/ruby/numeric.rb', line 68 def distance(num) self - num end |
#divide(num) ⇒ Object
72 73 74 75 76 |
# File 'lib/lite/ruby/numeric.rb', line 72 def divide(num) return num if num.zero? self / num end |
#equal_to?(num) ⇒ Boolean Also known as: eq?
78 79 80 |
# File 'lib/lite/ruby/numeric.rb', line 78 def equal_to?(num) self == num end |
#fraction ⇒ Object
84 85 86 |
# File 'lib/lite/ruby/numeric.rb', line 84 def fraction (self - truncate).abs end |
#fraction? ⇒ Boolean
88 89 90 |
# File 'lib/lite/ruby/numeric.rb', line 88 def fraction? fraction != 0.0 end |
#greater_than?(num) ⇒ Boolean Also known as: gt?
92 93 94 |
# File 'lib/lite/ruby/numeric.rb', line 92 def greater_than?(num) num < self end |
#greater_than_or_equal_to?(num) ⇒ Boolean Also known as: gteq?
98 99 100 |
# File 'lib/lite/ruby/numeric.rb', line 98 def greater_than_or_equal_to?(num) num <= self end |
#increment(amount = 1.0) ⇒ Object
104 105 106 |
# File 'lib/lite/ruby/numeric.rb', line 104 def increment(amount = 1.0) self + amount end |
#inside?(start, finish) ⇒ Boolean
108 109 110 |
# File 'lib/lite/ruby/numeric.rb', line 108 def inside?(start, finish) (start < self) && (finish > self) end |
#less_than?(num) ⇒ Boolean Also known as: lt?
112 113 114 |
# File 'lib/lite/ruby/numeric.rb', line 112 def less_than?(num) num > self end |
#less_than_or_equal_to?(num) ⇒ Boolean Also known as: lteq?
118 119 120 |
# File 'lib/lite/ruby/numeric.rb', line 118 def less_than_or_equal_to?(num) num >= self end |
#markdown_percentage(percent) ⇒ Object
124 125 126 |
# File 'lib/lite/ruby/numeric.rb', line 124 def markdown_percentage(percent) to_f * ((100.0 - percent.to_f) / 100.0) end |
#markup_percentage(percent) ⇒ Object
128 129 130 |
# File 'lib/lite/ruby/numeric.rb', line 128 def markup_percentage(percent) to_f + (to_f * (percent.to_f / 100.0)) end |
#multiple_of?(number) ⇒ Boolean
136 137 138 139 140 |
# File 'lib/lite/ruby/numeric.rb', line 136 def multiple_of?(number) return zero? if number.zero? modulo(number).zero? end |
#multiply(num) ⇒ Object
132 133 134 |
# File 'lib/lite/ruby/numeric.rb', line 132 def multiply(num) self * num end |
#not_equal_to?(num) ⇒ Boolean Also known as: not_eq?, inequal_to?, ineq?
142 143 144 |
# File 'lib/lite/ruby/numeric.rb', line 142 def not_equal_to?(num) self != num end |
#ordinal ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/lite/ruby/numeric.rb', line 150 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
161 162 163 |
# File 'lib/lite/ruby/numeric.rb', line 161 def ordinalize "#{self}#{ordinal}" end |
#outside?(start, finish) ⇒ Boolean
165 166 167 |
# File 'lib/lite/ruby/numeric.rb', line 165 def outside?(start, finish) (start > self) || (finish < self) end |
#pad(options = {}) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/lite/ruby/numeric.rb', line 169 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/lite/ruby/numeric.rb', line 178 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
197 198 199 200 201 |
# File 'lib/lite/ruby/numeric.rb', line 197 def percentage_of(number) return 0 if zero? || number.zero? (self / number.to_f) * 100.0 end |
#power(num) ⇒ Object
203 204 205 |
# File 'lib/lite/ruby/numeric.rb', line 203 def power(num) self**num end |
#range(value) ⇒ Object Also known as: plus_minus
207 208 209 |
# File 'lib/lite/ruby/numeric.rb', line 207 def range(value) (self - value)..(self + value) end |
#root(num) ⇒ Object
213 214 215 |
# File 'lib/lite/ruby/numeric.rb', line 213 def root(num) self**(1.0 / num) end |
#round_down(num = 0) ⇒ Object
217 218 219 220 221 |
# File 'lib/lite/ruby/numeric.rb', line 217 def round_down(num = 0) int, dec = to_f.to_s.split('.') "#{int}.#{dec[0...num]}".to_f end |
#subtract(num) ⇒ Object
223 224 225 |
# File 'lib/lite/ruby/numeric.rb', line 223 def subtract(num) self - num end |
#to_currency(options = {}) ⇒ Object
227 228 229 230 231 |
# File 'lib/lite/ruby/numeric.rb', line 227 def to_currency( = {}) unit = [:unit] || '$' "#{unit}#{pad_precision(.only(:precision))}" end |
#to_nearest_value(values = []) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/lite/ruby/numeric.rb', line 233 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
249 250 251 252 253 |
# File 'lib/lite/ruby/numeric.rb', line 249 def to_percentage( = {}) unit = [:unit] || '%' "#{pad_precision(.only(:precision))}#{unit}" end |
#within?(number, epsilon = 0.01) ⇒ Boolean
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/lite/ruby/numeric.rb', line 255 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 |