Class: Numeric

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

Instance Method Summary collapse

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

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

Returns:

  • (Boolean)


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

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



31
32
33
# File 'lib/lite/ruby/numeric.rb', line 31

def decrement(amount = 1.0)
  self - amount
end

#delimit(options = {}) ⇒ Object



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

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



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

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

#distance(num) ⇒ Object



51
52
53
# File 'lib/lite/ruby/numeric.rb', line 51

def distance(num)
  self - num
end

#divide(num) ⇒ Object



55
56
57
58
59
# File 'lib/lite/ruby/numeric.rb', line 55

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

  self / num
end

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/lite/ruby/numeric.rb', line 61

def equal_to?(num)
  self == num
end

#fractionObject



65
66
67
# File 'lib/lite/ruby/numeric.rb', line 65

def fraction
  (self - truncate).abs
end

#fraction?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/lite/ruby/numeric.rb', line 69

def fraction?
  fraction.to_d != 0.0.to_d
end

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

Returns:

  • (Boolean)


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

def greater_than?(num)
  num < self
end

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

Returns:

  • (Boolean)


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

def greater_than_or_equal_to?(num)
  num <= self
end

#increment(amount = 1.0) ⇒ Object



81
82
83
# File 'lib/lite/ruby/numeric.rb', line 81

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/lite/ruby/numeric.rb', line 85

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

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/lite/ruby/numeric.rb', line 89

def less_than?(num)
  num > self
end

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/lite/ruby/numeric.rb', line 93

def less_than_or_equal_to?(num)
  num >= self
end

#markdown_percentage(percent) ⇒ Object



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

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

#markup_percentage(percent) ⇒ Object



101
102
103
# File 'lib/lite/ruby/numeric.rb', line 101

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

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  modulo(number).zero?
end

#multiply(num) ⇒ Object



105
106
107
# File 'lib/lite/ruby/numeric.rb', line 105

def multiply(num)
  self * num
end

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

Returns:

  • (Boolean)


115
116
117
# File 'lib/lite/ruby/numeric.rb', line 115

def not_equal_to?(num)
  self != num
end

#ordinalObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/lite/ruby/numeric.rb', line 119

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



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

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

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/lite/ruby/numeric.rb', line 134

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

#pad(options = {}) ⇒ Object



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

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/MethodLength



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lite/ruby/numeric.rb', line 146

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/MethodLength



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

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

  (self / number.to_f) * 100.0
end

#power(num) ⇒ Object



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

def power(num)
  self**num
end

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



174
175
176
# File 'lib/lite/ruby/numeric.rb', line 174

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

#root(num) ⇒ Object



178
179
180
# File 'lib/lite/ruby/numeric.rb', line 178

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

#round_down(num = 0) ⇒ Object



182
183
184
185
186
# File 'lib/lite/ruby/numeric.rb', line 182

def round_down(num = 0)
  int, dec = to_f.to_s.split('.')

  "#{int}.#{dec[0...num]}".to_f
end

#subtract(num) ⇒ Object



188
189
190
# File 'lib/lite/ruby/numeric.rb', line 188

def subtract(num)
  self - num
end

#to_currency(options = {}) ⇒ Object



192
193
194
195
196
# File 'lib/lite/ruby/numeric.rb', line 192

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

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

#to_nearest_value(values = []) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/lite/ruby/numeric.rb', line 198

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



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

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

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

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

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/lite/ruby/numeric.rb', line 220

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