Class: Numeric
Instance Method Summary collapse
- #add(num) ⇒ Object
- #at_least(lower) ⇒ Object
- #at_most(upper) ⇒ Object
- #close?(number, epsilon = 0.01) ⇒ Boolean
- #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?)
- #many? ⇒ Boolean
- #markdown_percentage(percent) ⇒ Object
- #markup_percentage(percent) ⇒ Object
- #multiple_of?(number) ⇒ Boolean
- #multiply(num) ⇒ Object
- #negate ⇒ Object
- #not_equal_to?(num) ⇒ Boolean (also: #inequal_to?, #ineq?, #not_eq?)
- #one? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #outside?(start, finish) ⇒ Boolean
- #pad(options = {}) ⇒ Object
-
#pad_precision(options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#percentage_of(number) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #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
- #to_range ⇒ 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 |
#at_least(lower) ⇒ Object
9 10 11 |
# File 'lib/lite/ruby/numeric.rb', line 9 def at_least(lower) self >= lower ? self : lower end |
#at_most(upper) ⇒ Object
13 14 15 |
# File 'lib/lite/ruby/numeric.rb', line 13 def at_most(upper) self <= upper ? self : upper end |
#close?(number, epsilon = 0.01) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/lite/ruby/numeric.rb', line 17 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
30 31 32 |
# File 'lib/lite/ruby/numeric.rb', line 30 def decrement(amount = 1.0) self - amount end |
#delimit(options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lite/ruby/numeric.rb', line 34 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
46 47 48 |
# File 'lib/lite/ruby/numeric.rb', line 46 def delta(num) (self - num).abs end |
#distance(num) ⇒ Object
50 51 52 |
# File 'lib/lite/ruby/numeric.rb', line 50 def distance(num) self - num end |
#divide(num) ⇒ Object
54 55 56 57 58 |
# File 'lib/lite/ruby/numeric.rb', line 54 def divide(num) return num if num.zero? self / num end |
#equal_to?(num) ⇒ Boolean Also known as: eq?
60 61 62 |
# File 'lib/lite/ruby/numeric.rb', line 60 def equal_to?(num) self == num end |
#fraction ⇒ Object
64 65 66 |
# File 'lib/lite/ruby/numeric.rb', line 64 def fraction (self - truncate).abs end |
#fraction? ⇒ Boolean
68 69 70 |
# File 'lib/lite/ruby/numeric.rb', line 68 def fraction? fraction.to_d != BigDecimal('0.0') end |
#greater_than?(num) ⇒ Boolean Also known as: gt?
72 73 74 |
# File 'lib/lite/ruby/numeric.rb', line 72 def greater_than?(num) num < self end |
#greater_than_or_equal_to?(num) ⇒ Boolean Also known as: gteq?
76 77 78 |
# File 'lib/lite/ruby/numeric.rb', line 76 def greater_than_or_equal_to?(num) num <= self end |
#increment(amount = 1.0) ⇒ Object
80 81 82 |
# File 'lib/lite/ruby/numeric.rb', line 80 def increment(amount = 1.0) self + amount end |
#inside?(start, finish) ⇒ Boolean
84 85 86 |
# File 'lib/lite/ruby/numeric.rb', line 84 def inside?(start, finish) (start < self) && (finish > self) end |
#less_than?(num) ⇒ Boolean Also known as: lt?
88 89 90 |
# File 'lib/lite/ruby/numeric.rb', line 88 def less_than?(num) num > self end |
#less_than_or_equal_to?(num) ⇒ Boolean Also known as: lteq?
92 93 94 |
# File 'lib/lite/ruby/numeric.rb', line 92 def less_than_or_equal_to?(num) num >= self end |
#many? ⇒ Boolean
96 97 98 |
# File 'lib/lite/ruby/numeric.rb', line 96 def many? to_f > 1.0 end |
#markdown_percentage(percent) ⇒ Object
100 101 102 |
# File 'lib/lite/ruby/numeric.rb', line 100 def markdown_percentage(percent) to_f * ((100.0 - percent.to_f) / 100.0) end |
#markup_percentage(percent) ⇒ Object
104 105 106 |
# File 'lib/lite/ruby/numeric.rb', line 104 def markup_percentage(percent) to_f + (to_f * (percent.to_f / 100.0)) end |
#multiple_of?(number) ⇒ Boolean
112 113 114 115 116 |
# File 'lib/lite/ruby/numeric.rb', line 112 def multiple_of?(number) return zero? if number.zero? modulo(number).zero? end |
#multiply(num) ⇒ Object
108 109 110 |
# File 'lib/lite/ruby/numeric.rb', line 108 def multiply(num) self * num end |
#negate ⇒ Object
118 119 120 |
# File 'lib/lite/ruby/numeric.rb', line 118 def negate -self end |
#not_equal_to?(num) ⇒ Boolean Also known as: inequal_to?, ineq?, not_eq?
122 123 124 |
# File 'lib/lite/ruby/numeric.rb', line 122 def not_equal_to?(num) self != num end |
#one? ⇒ Boolean
126 127 128 |
# File 'lib/lite/ruby/numeric.rb', line 126 def one? to_d == BigDecimal('1.0') end |
#ordinal ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/lite/ruby/numeric.rb', line 130 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
141 142 143 |
# File 'lib/lite/ruby/numeric.rb', line 141 def ordinalize "#{self}#{ordinal}" end |
#outside?(start, finish) ⇒ Boolean
145 146 147 |
# File 'lib/lite/ruby/numeric.rb', line 145 def outside?(start, finish) (start > self) || (finish < self) end |
#pad(options = {}) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/lite/ruby/numeric.rb', line 149 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/MethodLength
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/lite/ruby/numeric.rb', line 157 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/MethodLength
175 176 177 178 179 |
# File 'lib/lite/ruby/numeric.rb', line 175 def percentage_of(number) return 0 if zero? || number.zero? (self / number.to_f) * 100.0 end |
#power(num) ⇒ Object
181 182 183 |
# File 'lib/lite/ruby/numeric.rb', line 181 def power(num) self**num end |
#range(value) ⇒ Object Also known as: plus_minus
185 186 187 |
# File 'lib/lite/ruby/numeric.rb', line 185 def range(value) (self - value)..(self + value) end |
#root(num) ⇒ Object
189 190 191 |
# File 'lib/lite/ruby/numeric.rb', line 189 def root(num) self**(1.0 / num) end |
#round_down(num = 0) ⇒ Object
193 194 195 196 197 |
# File 'lib/lite/ruby/numeric.rb', line 193 def round_down(num = 0) int, dec = to_f.to_s.split('.') "#{int}.#{dec[0...num]}".to_f end |
#subtract(num) ⇒ Object
199 200 201 |
# File 'lib/lite/ruby/numeric.rb', line 199 def subtract(num) self - num end |
#to_currency(options = {}) ⇒ Object
203 204 205 206 207 |
# File 'lib/lite/ruby/numeric.rb', line 203 def to_currency( = {}) unit = [:unit] || '$' "#{unit}#{pad_precision(.only(:precision))}" end |
#to_nearest_value(values = []) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/lite/ruby/numeric.rb', line 209 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
225 226 227 228 229 |
# File 'lib/lite/ruby/numeric.rb', line 225 def to_percentage( = {}) unit = [:unit] || '%' "#{pad_precision(.only(:precision))}#{unit}" end |
#to_range ⇒ Object
231 232 233 |
# File 'lib/lite/ruby/numeric.rb', line 231 def to_range negative? ? (self..-self) : (-self..self) end |
#within?(number, epsilon = 0.01) ⇒ Boolean
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/lite/ruby/numeric.rb', line 235 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 |