lite-ruby 1.0.14 → 1.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
data/lib/lite/ruby/numeric.rb
CHANGED
@@ -1,261 +1,263 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
4
|
+
class Numeric
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def at_least(lower)
|
10
|
-
self >= lower ? self : lower
|
11
|
-
end
|
6
|
+
def add(num)
|
7
|
+
self + num
|
8
|
+
end
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
def at_least(lower)
|
11
|
+
self >= lower ? self : lower
|
12
|
+
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
if minimum.is_a?(Range)
|
20
|
-
maximum = minimum.max
|
21
|
-
minimum = minimum.min
|
14
|
+
def at_most(upper)
|
15
|
+
self <= upper ? self : upper
|
22
16
|
end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
18
|
+
# rubocop:disable Metrics/MethodLength
|
19
|
+
def clamp(minimum, maximum = nil)
|
20
|
+
if minimum.is_a?(Range)
|
21
|
+
maximum = minimum.max
|
22
|
+
minimum = minimum.min
|
23
|
+
end
|
24
|
+
|
25
|
+
if minimum > self
|
26
|
+
minimum
|
27
|
+
elsif maximum < self
|
28
|
+
maximum
|
29
|
+
else
|
30
|
+
self
|
31
|
+
end
|
30
32
|
end
|
31
|
-
|
32
|
-
# rubocop:enable Metrics/MethodLength
|
33
|
+
# rubocop:enable Metrics/MethodLength
|
33
34
|
|
34
|
-
|
35
|
-
|
35
|
+
def close?(number, epsilon = 0.01)
|
36
|
+
return self == number if epsilon.zero?
|
36
37
|
|
37
|
-
|
38
|
-
|
38
|
+
a = to_f
|
39
|
+
b = number.to_f
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
if a.zero? || b.zero?
|
42
|
+
(a - b).abs < epsilon
|
43
|
+
else
|
44
|
+
(a / b - 1).abs < epsilon
|
45
|
+
end
|
44
46
|
end
|
45
|
-
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
def decrement(amount = 1.0)
|
49
|
+
self - amount
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def delimit(options = {})
|
53
|
+
delimiter = options[:delimiter] || ','
|
54
|
+
separator = options[:separator] || '.'
|
54
55
|
|
55
|
-
|
56
|
-
|
56
|
+
digits, decimals = to_s.split('.')
|
57
|
+
digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse
|
57
58
|
|
58
|
-
|
59
|
+
return digits unless decimals
|
59
60
|
|
60
|
-
|
61
|
-
|
61
|
+
[digits, decimals].join(separator)
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def delta(num)
|
65
|
+
(self - num).abs
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
def distance(num)
|
69
|
+
self - num
|
70
|
+
end
|
70
71
|
|
71
|
-
|
72
|
-
|
72
|
+
def divide(num)
|
73
|
+
return num if num.zero?
|
73
74
|
|
74
|
-
|
75
|
-
|
75
|
+
self / num
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
def equal_to?(num)
|
79
|
+
self == num
|
80
|
+
end
|
80
81
|
|
81
|
-
|
82
|
+
alias eq? equal_to?
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
def fraction
|
85
|
+
(self - truncate).abs
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
88
|
+
def fraction?
|
89
|
+
fraction != 0.0
|
90
|
+
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
92
|
+
def greater_than?(num)
|
93
|
+
num < self
|
94
|
+
end
|
94
95
|
|
95
|
-
|
96
|
+
alias gt? greater_than?
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
def greater_than_or_equal_to?(num)
|
99
|
+
num <= self
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
+
alias gteq? greater_than_or_equal_to?
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
104
|
+
def increment(amount = 1.0)
|
105
|
+
self + amount
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
108
|
+
def inside?(start, finish)
|
109
|
+
(start < self) && (finish > self)
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
def less_than?(num)
|
113
|
+
num > self
|
114
|
+
end
|
114
115
|
|
115
|
-
|
116
|
+
alias lt? less_than?
|
116
117
|
|
117
|
-
|
118
|
-
|
119
|
-
|
118
|
+
def less_than_or_equal_to?(num)
|
119
|
+
num >= self
|
120
|
+
end
|
120
121
|
|
121
|
-
|
122
|
+
alias lteq? less_than_or_equal_to?
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
124
|
+
def markdown_percentage(percent)
|
125
|
+
to_f * ((100.0 - percent.to_f) / 100.0)
|
126
|
+
end
|
126
127
|
|
127
|
-
|
128
|
-
|
129
|
-
|
128
|
+
def markup_percentage(percent)
|
129
|
+
to_f + (to_f * (percent.to_f / 100.0))
|
130
|
+
end
|
130
131
|
|
131
|
-
|
132
|
-
|
133
|
-
|
132
|
+
def multiply(num)
|
133
|
+
self * num
|
134
|
+
end
|
134
135
|
|
135
|
-
|
136
|
-
|
136
|
+
def multiple_of?(number)
|
137
|
+
return zero? if number.zero?
|
137
138
|
|
138
|
-
|
139
|
-
|
139
|
+
modulo(number).zero?
|
140
|
+
end
|
140
141
|
|
141
|
-
|
142
|
-
|
143
|
-
|
142
|
+
def not_equal_to?(num)
|
143
|
+
self != num
|
144
|
+
end
|
144
145
|
|
145
|
-
|
146
|
-
|
147
|
-
|
146
|
+
alias not_eq? not_equal_to?
|
147
|
+
alias inequal_to? not_equal_to?
|
148
|
+
alias ineq? not_equal_to?
|
148
149
|
|
149
|
-
|
150
|
-
|
150
|
+
def ordinal
|
151
|
+
return 'th' if (11..13).cover?(abs % 100)
|
151
152
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
153
|
+
case abs % 10
|
154
|
+
when 1 then 'st'
|
155
|
+
when 2 then 'nd'
|
156
|
+
when 3 then 'rd'
|
157
|
+
else 'th'
|
158
|
+
end
|
157
159
|
end
|
158
|
-
end
|
159
160
|
|
160
|
-
|
161
|
-
|
162
|
-
|
161
|
+
def ordinalize
|
162
|
+
"#{self}#{ordinal}"
|
163
|
+
end
|
163
164
|
|
164
|
-
|
165
|
-
|
166
|
-
|
165
|
+
def outside?(start, finish)
|
166
|
+
(start > self) || (finish < self)
|
167
|
+
end
|
167
168
|
|
168
|
-
|
169
|
-
|
170
|
-
|
169
|
+
def pad(options = {})
|
170
|
+
pad_number = options[:pad_number] || 0
|
171
|
+
precision = options[:precision] || 3
|
171
172
|
|
172
|
-
|
173
|
-
|
173
|
+
to_s.rjust(precision, pad_number.to_s)
|
174
|
+
end
|
174
175
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
176
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
177
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
178
|
+
def pad_precision(options = {})
|
179
|
+
pad_number = options[:pad_number] || 0
|
180
|
+
precision = options[:precision] || 2
|
181
|
+
separator = options[:separator] || '.'
|
182
|
+
string = to_s
|
183
|
+
|
184
|
+
string << separator unless string.include?(separator)
|
185
|
+
ljust_count = string.split(separator).first.length
|
186
|
+
ljust_count += (string.count(separator) + precision) if precision.positive?
|
187
|
+
|
188
|
+
if ljust_count >= string.length
|
189
|
+
string.ljust(ljust_count, pad_number.to_s)
|
190
|
+
else
|
191
|
+
string[0..(ljust_count - 1)]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
195
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
182
196
|
|
183
|
-
|
184
|
-
|
185
|
-
ljust_count += (string.count(separator) + precision) if precision.positive?
|
197
|
+
def percentage_of(number)
|
198
|
+
return 0 if zero? || number.zero?
|
186
199
|
|
187
|
-
|
188
|
-
string.ljust(ljust_count, pad_number.to_s)
|
189
|
-
else
|
190
|
-
string[0..(ljust_count - 1)]
|
200
|
+
(self / number.to_f) * 100.0
|
191
201
|
end
|
192
|
-
end
|
193
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
194
|
-
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
195
202
|
|
196
|
-
|
197
|
-
|
203
|
+
def power(num)
|
204
|
+
self**num
|
205
|
+
end
|
198
206
|
|
199
|
-
(
|
200
|
-
|
207
|
+
def range(value)
|
208
|
+
(self - value)..(self + value)
|
209
|
+
end
|
201
210
|
|
202
|
-
|
203
|
-
self**num
|
204
|
-
end
|
211
|
+
alias plus_minus range
|
205
212
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
alias plus_minus range
|
213
|
+
def root(num)
|
214
|
+
self**(1.0 / num)
|
215
|
+
end
|
211
216
|
|
212
|
-
|
213
|
-
|
214
|
-
|
217
|
+
def subtract(num)
|
218
|
+
self - num
|
219
|
+
end
|
215
220
|
|
216
|
-
|
217
|
-
|
218
|
-
end
|
221
|
+
def to_currency(options = {})
|
222
|
+
unit = options[:unit] || '$'
|
219
223
|
|
220
|
-
|
221
|
-
|
224
|
+
"#{unit}#{pad_precision(options.only(:precision))}"
|
225
|
+
end
|
222
226
|
|
223
|
-
|
224
|
-
|
227
|
+
def to_nearest_value(values = [])
|
228
|
+
return self if values.length.zero?
|
225
229
|
|
226
|
-
|
227
|
-
|
230
|
+
value = values.first
|
231
|
+
difference = (self - value).abs
|
228
232
|
|
229
|
-
|
230
|
-
|
233
|
+
values.each do |val|
|
234
|
+
next unless (self - val).abs < difference
|
231
235
|
|
232
|
-
|
233
|
-
|
236
|
+
difference = (self - val).abs
|
237
|
+
value = val
|
238
|
+
end
|
234
239
|
|
235
|
-
|
236
|
-
value = val
|
240
|
+
value
|
237
241
|
end
|
238
242
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
def to_percentage(options = {})
|
243
|
-
unit = options[:unit] || '%'
|
243
|
+
def to_percentage(options = {})
|
244
|
+
unit = options[:unit] || '%'
|
244
245
|
|
245
|
-
|
246
|
-
|
246
|
+
"#{pad_precision(options.only(:precision))}#{unit}"
|
247
|
+
end
|
247
248
|
|
248
|
-
|
249
|
-
|
249
|
+
def within?(number, epsilon = 0.01)
|
250
|
+
return number == self if epsilon.zero?
|
250
251
|
|
251
|
-
|
252
|
-
|
252
|
+
alpha = to_f
|
253
|
+
beta = number.to_f
|
253
254
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
255
|
+
if alpha.zero? || beta.zero?
|
256
|
+
(alpha - beta).abs < epsilon
|
257
|
+
else
|
258
|
+
(alpha / beta - 1).abs < epsilon
|
259
|
+
end
|
258
260
|
end
|
259
|
-
end
|
260
261
|
|
262
|
+
end
|
261
263
|
end
|