Class: Numeric

Inherits:
Object show all
Defined in:
lib/lite/ruby/numeric.rb

Instance Method Summary collapse

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

#clamp(minimum, maximum = nil) ⇒ Object

rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lite/ruby/numeric.rb', line 18

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

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lite/ruby/numeric.rb', line 34

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



47
48
49
# File 'lib/lite/ruby/numeric.rb', line 47

def decrement(amount = 1.0)
  self - amount
end

#delimit(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lite/ruby/numeric.rb', line 51

def delimit(options = {})
  delimiter = options[:delimiter] || ','
  separator = options[: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



63
64
65
# File 'lib/lite/ruby/numeric.rb', line 63

def delta(num)
  (self - num).abs
end

#distance(num) ⇒ Object



67
68
69
# File 'lib/lite/ruby/numeric.rb', line 67

def distance(num)
  self - num
end

#divide(num) ⇒ Object



71
72
73
74
75
# File 'lib/lite/ruby/numeric.rb', line 71

def divide(num)
  return num if num.zero?

  self / num
end

#equal_to?(num) ⇒ Boolean Also known as: eq?

Returns:

  • (Boolean)


77
78
79
# File 'lib/lite/ruby/numeric.rb', line 77

def equal_to?(num)
  self == num
end

#fractionObject



83
84
85
# File 'lib/lite/ruby/numeric.rb', line 83

def fraction
  (self - truncate).abs
end

#fraction?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/lite/ruby/numeric.rb', line 87

def fraction?
  fraction != 0.0
end

#greater_than?(num) ⇒ Boolean Also known as: gt?

Returns:

  • (Boolean)


91
92
93
# File 'lib/lite/ruby/numeric.rb', line 91

def greater_than?(num)
  num < self
end

#greater_than_or_equal_to?(num) ⇒ Boolean Also known as: gteq?

Returns:

  • (Boolean)


97
98
99
# File 'lib/lite/ruby/numeric.rb', line 97

def greater_than_or_equal_to?(num)
  num <= self
end

#increment(amount = 1.0) ⇒ Object



103
104
105
# File 'lib/lite/ruby/numeric.rb', line 103

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/lite/ruby/numeric.rb', line 107

def inside?(start, finish)
  (start < self) && (finish > self)
end

#less_than?(num) ⇒ Boolean Also known as: lt?

Returns:

  • (Boolean)


111
112
113
# File 'lib/lite/ruby/numeric.rb', line 111

def less_than?(num)
  num > self
end

#less_than_or_equal_to?(num) ⇒ Boolean Also known as: lteq?

Returns:

  • (Boolean)


117
118
119
# File 'lib/lite/ruby/numeric.rb', line 117

def less_than_or_equal_to?(num)
  num >= self
end

#markdown_percentage(percent) ⇒ Object



123
124
125
# File 'lib/lite/ruby/numeric.rb', line 123

def markdown_percentage(percent)
  to_f * ((100.0 - percent.to_f) / 100.0)
end

#markup_percentage(percent) ⇒ Object



127
128
129
# File 'lib/lite/ruby/numeric.rb', line 127

def markup_percentage(percent)
  to_f + (to_f * (percent.to_f / 100.0))
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
# File 'lib/lite/ruby/numeric.rb', line 135

def multiple_of?(number)
  return zero? if number.zero?

  modulo(number).zero?
end

#multiply(num) ⇒ Object



131
132
133
# File 'lib/lite/ruby/numeric.rb', line 131

def multiply(num)
  self * num
end

#not_equal_to?(num) ⇒ Boolean Also known as: not_eq?, inequal_to?, ineq?

Returns:

  • (Boolean)


141
142
143
# File 'lib/lite/ruby/numeric.rb', line 141

def not_equal_to?(num)
  self != num
end

#ordinalObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/lite/ruby/numeric.rb', line 149

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

#ordinalizeObject



160
161
162
# File 'lib/lite/ruby/numeric.rb', line 160

def ordinalize
  "#{self}#{ordinal}"
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/lite/ruby/numeric.rb', line 164

def outside?(start, finish)
  (start > self) || (finish < self)
end

#pad(options = {}) ⇒ Object



168
169
170
171
172
173
# File 'lib/lite/ruby/numeric.rb', line 168

def pad(options = {})
  pad_number = options[:pad_number] || 0
  precision = options[: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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lite/ruby/numeric.rb', line 177

def pad_precision(options = {})
  pad_number = options[:pad_number] || 0
  precision = options[:precision] || 2
  separator = options[: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



196
197
198
199
200
# File 'lib/lite/ruby/numeric.rb', line 196

def percentage_of(number)
  return 0 if zero? || number.zero?

  (self / number.to_f) * 100.0
end

#power(num) ⇒ Object



202
203
204
# File 'lib/lite/ruby/numeric.rb', line 202

def power(num)
  self**num
end

#range(value) ⇒ Object Also known as: plus_minus



206
207
208
# File 'lib/lite/ruby/numeric.rb', line 206

def range(value)
  (self - value)..(self + value)
end

#root(num) ⇒ Object



212
213
214
# File 'lib/lite/ruby/numeric.rb', line 212

def root(num)
  self**(1.0 / num)
end

#subtract(num) ⇒ Object



216
217
218
# File 'lib/lite/ruby/numeric.rb', line 216

def subtract(num)
  self - num
end

#to_currency(options = {}) ⇒ Object



220
221
222
223
224
# File 'lib/lite/ruby/numeric.rb', line 220

def to_currency(options = {})
  unit = options[:unit] || '$'

  "#{unit}#{pad_precision(options.only(:precision))}"
end

#to_nearest_value(values = []) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/lite/ruby/numeric.rb', line 226

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



242
243
244
245
246
# File 'lib/lite/ruby/numeric.rb', line 242

def to_percentage(options = {})
  unit = options[:unit] || '%'

  "#{pad_precision(options.only(:precision))}#{unit}"
end

#within?(number, epsilon = 0.01) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/lite/ruby/numeric.rb', line 248

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