lite-ruby 1.3.3 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/Gemfile.lock +8 -8
- data/README.md +26 -6
- data/docs/HASH.md +13 -0
- data/docs/NUMERIC.md +48 -2
- data/lib/generators/lite/ruby/templates/install.rb +18 -6
- data/lib/lite/ruby.rb +1 -14
- data/lib/lite/ruby/array.rb +218 -220
- data/lib/lite/ruby/boolean.rb +17 -19
- data/lib/lite/ruby/date.rb +13 -20
- data/lib/lite/ruby/enumerable.rb +153 -155
- data/lib/lite/ruby/formats/date_stamps.yml +34 -0
- data/lib/lite/ruby/formats/date_units.yml +42 -0
- data/lib/lite/ruby/formats/integer_roman_numerals.yml +13 -0
- data/lib/lite/ruby/formats/string_transliterations.yml +189 -0
- data/lib/lite/ruby/formats/time_stamps.yml +42 -0
- data/lib/lite/ruby/formats/time_units.yml +28 -0
- data/lib/lite/ruby/hash.rb +313 -301
- data/lib/lite/ruby/helpers/date_time_helper.rb +24 -0
- data/lib/lite/ruby/integer.rb +62 -63
- data/lib/lite/ruby/kernel.rb +19 -21
- data/lib/lite/ruby/monkey_patches.rb +16 -0
- data/lib/lite/ruby/numeric.rb +192 -177
- data/lib/lite/ruby/object.rb +128 -130
- data/lib/lite/ruby/open_struct.rb +17 -19
- data/lib/lite/ruby/range.rb +19 -21
- data/lib/lite/ruby/safe/string.rb +8 -7
- data/lib/lite/ruby/string.rb +348 -384
- data/lib/lite/ruby/struct.rb +7 -9
- data/lib/lite/ruby/time.rb +25 -30
- data/lib/lite/ruby/version.rb +1 -1
- metadata +10 -5
- data/lib/lite/ruby/configuration.rb +0 -38
- data/lib/lite/ruby/helpers/date_helper.rb +0 -107
- data/lib/lite/ruby/helpers/time_helper.rb +0 -86
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29e7241b666924e764cb6109751dde2dd55b5964a1a5ee1656e20d44373ab516
|
4
|
+
data.tar.gz: 875f644d9a7e9bc392b093c0e9d030321e1a1d3230dbd9214f3225df4cdff613
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dc8caff79b7ec516abf91f4eaad29b5377ee092d214bed010b1a56cb176abb8c040b1c79e44bf0947cdb6ab20935586378a7d9f82ea85f51ff1e4dbd561342a
|
7
|
+
data.tar.gz: d117f070aaefd4a835efce753781a28f330e009863a5843406430e9bc786750efa8f071f66ce189a1c79153e2a658979302844b37d15e4bd01e844b4e6d3fd4c
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [2.0.3] - 2021-08-05
|
10
|
+
### Changed
|
11
|
+
- Added Numeric => `negate`
|
12
|
+
- Added Numeric => `many?`
|
13
|
+
- Added Numeric => `one?`
|
14
|
+
- Added Numeric => `none?` to `zero?` alias
|
15
|
+
- Added Numeric => `to_range`
|
16
|
+
|
17
|
+
## [2.0.2] - 2021-07-22
|
18
|
+
### Changed
|
19
|
+
- Changed `all.rb` => `monkey_patches.rb`
|
20
|
+
|
21
|
+
## [2.0.1] - 2021-07-22
|
22
|
+
### Added
|
23
|
+
- Added `all.rb` for monkey patching everything with one command.
|
24
|
+
|
25
|
+
## [2.0.0] - 2021-07-22
|
26
|
+
### Added
|
27
|
+
- Added Hash => `deep_key?`
|
28
|
+
### Changed
|
29
|
+
- Update install generator to reflect configuration changes
|
30
|
+
### Removed
|
31
|
+
- Removed configuration to use explicit inclusions
|
32
|
+
|
9
33
|
## [1.3.3] - 2021-07-21
|
10
34
|
### Changed
|
11
35
|
- Improved generator support
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lite-ruby (
|
4
|
+
lite-ruby (2.0.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -40,14 +40,14 @@ GEM
|
|
40
40
|
railties (>= 3.0.0)
|
41
41
|
i18n (1.8.10)
|
42
42
|
concurrent-ruby (~> 1.0)
|
43
|
-
loofah (2.
|
43
|
+
loofah (2.11.0)
|
44
44
|
crass (~> 1.0.2)
|
45
45
|
nokogiri (>= 1.5.9)
|
46
46
|
method_source (1.0.0)
|
47
|
-
mini_portile2 (2.
|
47
|
+
mini_portile2 (2.6.1)
|
48
48
|
minitest (5.14.4)
|
49
|
-
nokogiri (1.
|
50
|
-
mini_portile2 (~> 2.
|
49
|
+
nokogiri (1.12.2)
|
50
|
+
mini_portile2 (~> 2.6.1)
|
51
51
|
racc (~> 1.4)
|
52
52
|
parallel (1.20.1)
|
53
53
|
parser (3.0.2.0)
|
@@ -84,13 +84,13 @@ GEM
|
|
84
84
|
diff-lcs (>= 1.2.0, < 2.0)
|
85
85
|
rspec-support (~> 3.10.0)
|
86
86
|
rspec-support (3.10.2)
|
87
|
-
rubocop (1.18.
|
87
|
+
rubocop (1.18.4)
|
88
88
|
parallel (~> 1.10)
|
89
89
|
parser (>= 3.0.0.0)
|
90
90
|
rainbow (>= 2.2.2, < 4.0)
|
91
91
|
regexp_parser (>= 1.8, < 3.0)
|
92
92
|
rexml
|
93
|
-
rubocop-ast (>= 1.
|
93
|
+
rubocop-ast (>= 1.8.0, < 2.0)
|
94
94
|
ruby-progressbar (~> 1.7)
|
95
95
|
unicode-display_width (>= 1.4.0, < 3.0)
|
96
96
|
rubocop-ast (1.8.0)
|
@@ -104,7 +104,7 @@ GEM
|
|
104
104
|
rubocop (~> 1.0)
|
105
105
|
rubocop-ast (>= 1.1.0)
|
106
106
|
ruby-progressbar (1.11.0)
|
107
|
-
ruby_parser (3.
|
107
|
+
ruby_parser (3.17.0)
|
108
108
|
sexp_processor (~> 4.15, >= 4.15.1)
|
109
109
|
sexp_processor (4.15.3)
|
110
110
|
thor (1.1.0)
|
data/README.md
CHANGED
@@ -33,16 +33,36 @@ Or install it yourself as:
|
|
33
33
|
|
34
34
|
## Configurations
|
35
35
|
|
36
|
+
Any and all monkey patches must be explicitly included anywhere you want to use it.
|
37
|
+
|
38
|
+
To globally use the monkey patches, just create an initializer requiring them.
|
39
|
+
|
36
40
|
`rails g lite:ruby:install` will generate the following file:
|
37
41
|
`../config/initalizers/lite_ruby.rb`
|
38
42
|
|
43
|
+
They can be disabled by commenting any of them out.
|
44
|
+
|
39
45
|
```ruby
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
+
# frozen_string_literal: true
|
47
|
+
|
48
|
+
require 'lite/ruby/monkey_patches'
|
49
|
+
|
50
|
+
# - or -
|
51
|
+
|
52
|
+
# require 'lite/ruby/array'
|
53
|
+
# require 'lite/ruby/boolean'
|
54
|
+
# require 'lite/ruby/date'
|
55
|
+
# require 'lite/ruby/enumerable'
|
56
|
+
# require 'lite/ruby/hash'
|
57
|
+
# require 'lite/ruby/integer'
|
58
|
+
# require 'lite/ruby/kernel'
|
59
|
+
# require 'lite/ruby/numeric'
|
60
|
+
# require 'lite/ruby/object'
|
61
|
+
# require 'lite/ruby/open_struct'
|
62
|
+
# require 'lite/ruby/range'
|
63
|
+
# require 'lite/ruby/string'
|
64
|
+
# require 'lite/ruby/struct'
|
65
|
+
# require 'lite/ruby/time'
|
46
66
|
```
|
47
67
|
|
48
68
|
## Extensions
|
data/docs/HASH.md
CHANGED
@@ -160,6 +160,19 @@ h2 = { a: false, b: { x: [3, 4, 5] } }
|
|
160
160
|
h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
161
161
|
```
|
162
162
|
|
163
|
+
`deep_key`
|
164
|
+
------
|
165
|
+
Returns if a set of nested keys exists.
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
h1 = { a: { c: { d: 2 } }, b: 1 }
|
169
|
+
|
170
|
+
h1.deep_key?(:a) #=> true
|
171
|
+
h1.deep_key?(:a, :c, :d) #=> true
|
172
|
+
h1.deep_key?(:x) #=> false
|
173
|
+
h1.deep_key?(:a, :c, :x) #=> false
|
174
|
+
```
|
175
|
+
|
163
176
|
`delete_unless`
|
164
177
|
------
|
165
178
|
Inverse of `delete_if`.
|
data/docs/NUMERIC.md
CHANGED
@@ -149,6 +149,15 @@ Returns if self is less than or equal to n.
|
|
149
149
|
3.less_than_or_equal_to?(4) #=> true
|
150
150
|
```
|
151
151
|
|
152
|
+
`many?`
|
153
|
+
------
|
154
|
+
Returns if a number is greater than one.
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
4.many? #=> true
|
158
|
+
1.many? #=> false
|
159
|
+
```
|
160
|
+
|
152
161
|
`markdown_percentage`
|
153
162
|
------
|
154
163
|
Returns the difference of a number and a percentage of it.
|
@@ -182,6 +191,24 @@ Returns if a number can be evenly divided by n.
|
|
182
191
|
7.multiple_of?(3) #=> false
|
183
192
|
```
|
184
193
|
|
194
|
+
`negate`
|
195
|
+
------
|
196
|
+
Returns the negation of a number.
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
4.negate #=> -4
|
200
|
+
-2.negate #=> 2
|
201
|
+
```
|
202
|
+
|
203
|
+
`none?` aka `zero?`
|
204
|
+
------
|
205
|
+
Returns if a number is to zero.
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
0.none? #=> true
|
209
|
+
1.none? #=> false
|
210
|
+
```
|
211
|
+
|
185
212
|
`not_equal_to?` aka `not_eq?` aka `inequal_to?` aka `ineq?`
|
186
213
|
------
|
187
214
|
Returns if not matching equality using `!=`.
|
@@ -191,6 +218,16 @@ Returns if not matching equality using `!=`.
|
|
191
218
|
3.not_equal_to?(3) #=> false
|
192
219
|
```
|
193
220
|
|
221
|
+
`one?`
|
222
|
+
------
|
223
|
+
Returns if a number is equal to one.
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
1.one? #=> true
|
227
|
+
1.0.one? #=> true
|
228
|
+
4.one? #=> false
|
229
|
+
```
|
230
|
+
|
194
231
|
`ordinal`
|
195
232
|
------
|
196
233
|
Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
@@ -317,15 +354,24 @@ unit | string | '$'
|
|
317
354
|
3.to_currency(unit: '@') #=> '@3.00'
|
318
355
|
```
|
319
356
|
|
320
|
-
`
|
357
|
+
`to_nearest_value`
|
321
358
|
------
|
322
|
-
|
359
|
+
Returns the value in values that is nearest to the number.
|
323
360
|
|
324
361
|
```ruby
|
325
362
|
5.to_nearest_value([1, 3, 6, 9]) #=> 6
|
326
363
|
3.5.to_nearest_value([3.0, 3.3, 3.6, 3.9]) #=> 3.6
|
327
364
|
```
|
328
365
|
|
366
|
+
`to_range`
|
367
|
+
------
|
368
|
+
Returns a range of a number from negative to positive.
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
5.to_range #=> -5..5
|
372
|
+
-5.to_range #=> -5..5
|
373
|
+
```
|
374
|
+
|
329
375
|
`to_percentage`
|
330
376
|
------
|
331
377
|
Converts a number to percentage string.
|
@@ -1,8 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
require 'lite/ruby/monkey_patches'
|
4
|
+
|
5
|
+
# - or -
|
6
|
+
|
7
|
+
# require 'lite/ruby/array'
|
8
|
+
# require 'lite/ruby/boolean'
|
9
|
+
# require 'lite/ruby/date'
|
10
|
+
# require 'lite/ruby/enumerable'
|
11
|
+
# require 'lite/ruby/hash'
|
12
|
+
# require 'lite/ruby/integer'
|
13
|
+
# require 'lite/ruby/kernel'
|
14
|
+
# require 'lite/ruby/numeric'
|
15
|
+
# require 'lite/ruby/object'
|
16
|
+
# require 'lite/ruby/open_struct'
|
17
|
+
# require 'lite/ruby/range'
|
18
|
+
# require 'lite/ruby/string'
|
19
|
+
# require 'lite/ruby/struct'
|
20
|
+
# require 'lite/ruby/time'
|
data/lib/lite/ruby.rb
CHANGED
@@ -2,17 +2,4 @@
|
|
2
2
|
|
3
3
|
require 'generators/lite/ruby/install_generator' if defined?(Rails::Generators)
|
4
4
|
|
5
|
-
|
6
|
-
require "lite/ruby/#{filename}"
|
7
|
-
end
|
8
|
-
|
9
|
-
%w[date time].each do |filename|
|
10
|
-
require "lite/ruby/helpers/#{filename}_helper"
|
11
|
-
end
|
12
|
-
|
13
|
-
%w[
|
14
|
-
array boolean date enumerable hash integer kernel numeric object open_struct range string
|
15
|
-
struct time
|
16
|
-
].each do |filename|
|
17
|
-
require "lite/ruby/#{filename}"
|
18
|
-
end
|
5
|
+
require 'lite/ruby/version'
|
data/lib/lite/ruby/array.rb
CHANGED
@@ -1,301 +1,299 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Array
|
3
|
+
class Array
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
def assert_min_values!(*valid_values)
|
6
|
+
return self if empty?
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
valid_values.each do |value|
|
9
|
+
next if include?(value)
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
self
|
11
|
+
raise ArgumentError,
|
12
|
+
"Missing value: #{value.inspect}. " \
|
13
|
+
"Minimum values are: #{valid_values.map(&:inspect).join(', ')}"
|
18
14
|
end
|
19
15
|
|
20
|
-
|
21
|
-
|
16
|
+
self
|
17
|
+
end
|
22
18
|
|
23
|
-
|
24
|
-
|
19
|
+
def assert_all_min_values!(*valid_values)
|
20
|
+
return assert_min_values!(*valid_values) unless empty?
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
next if valid_values.include?(value)
|
22
|
+
raise ArgumentError, 'An empty array is not allowed'
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
25
|
+
def assert_valid_values!(*valid_values)
|
26
|
+
each do |value|
|
27
|
+
next if valid_values.include?(value)
|
28
|
+
|
29
|
+
raise ArgumentError,
|
30
|
+
"Invalid value: #{value.inspect}. " \
|
31
|
+
"Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
|
34
32
|
end
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
35
|
+
def assert_all_valid_values!(*valid_values)
|
36
|
+
return assert_valid_values!(*valid_values) unless empty?
|
38
37
|
|
39
|
-
|
40
|
-
|
38
|
+
raise ArgumentError, 'An empty array is not allowed'
|
39
|
+
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
def assert_value_presence!
|
42
|
+
each do |value|
|
43
|
+
next if value.respond_to?(:present?) ? value.present? : value
|
45
44
|
|
46
|
-
|
47
|
-
end
|
45
|
+
raise ArgumentError, "A #{value.inspect} value is not allowed"
|
48
46
|
end
|
47
|
+
end
|
49
48
|
|
50
|
-
|
51
|
-
|
49
|
+
def assert_all_value_presence!
|
50
|
+
return assert_value_presence! unless empty?
|
52
51
|
|
53
|
-
|
54
|
-
|
52
|
+
raise ArgumentError, 'An empty array is not allowed'
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
55
|
+
def after(value)
|
56
|
+
return unless include?(value)
|
58
57
|
|
59
|
-
|
60
|
-
|
58
|
+
self[(index(value) + 1) % size]
|
59
|
+
end
|
61
60
|
|
62
|
-
|
63
|
-
|
61
|
+
def all_after(value)
|
62
|
+
return unless include?(value)
|
64
63
|
|
65
|
-
|
66
|
-
|
64
|
+
i = index(value)
|
65
|
+
return if i == (size - 1)
|
67
66
|
|
68
|
-
|
69
|
-
|
67
|
+
self[(i + 1)..-1]
|
68
|
+
end
|
70
69
|
|
71
|
-
|
72
|
-
|
70
|
+
def all_before(value)
|
71
|
+
return unless include?(value)
|
73
72
|
|
74
|
-
|
75
|
-
|
73
|
+
i = index(value)
|
74
|
+
return if i.zero?
|
76
75
|
|
77
|
-
|
78
|
-
|
76
|
+
self[0..(i - 1)]
|
77
|
+
end
|
79
78
|
|
80
|
-
|
81
|
-
|
79
|
+
def before(value)
|
80
|
+
return unless include?(value)
|
82
81
|
|
83
|
-
|
84
|
-
|
82
|
+
self[(index(value) - 1) % size]
|
83
|
+
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
else
|
95
|
-
self << { args[0] => args[1] }
|
96
|
-
end
|
85
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
86
|
+
# rubocop:disable Style/GuardClause, Style/IfInsideElse
|
87
|
+
def bury(*args)
|
88
|
+
if args.count < 2
|
89
|
+
raise ArgumentError, '2 or more arguments required'
|
90
|
+
elsif args.count == 2
|
91
|
+
if args[0].is_a?(Integer)
|
92
|
+
self[args[0]] = args[1]
|
97
93
|
else
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
94
|
+
self << { args[0] => args[1] }
|
95
|
+
end
|
96
|
+
else
|
97
|
+
if args[0].is_a?(Integer)
|
98
|
+
arg = args.shift
|
99
|
+
self[arg] = [] unless self[arg]
|
100
|
+
self[arg].bury(*args)
|
101
|
+
else
|
102
|
+
self << {}.bury(*args)
|
105
103
|
end
|
106
|
-
|
107
|
-
self
|
108
|
-
end
|
109
|
-
# rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
|
110
|
-
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
111
|
-
|
112
|
-
def contains_all?(other)
|
113
|
-
(other & self) == self
|
114
104
|
end
|
115
105
|
|
116
|
-
|
117
|
-
|
118
|
-
|
106
|
+
self
|
107
|
+
end
|
108
|
+
# rubocop:enable Style/GuardClause, Style/IfInsideElse
|
109
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
119
110
|
|
120
|
-
|
121
|
-
|
122
|
-
|
111
|
+
def contains_all?(other)
|
112
|
+
(other & self) == self
|
113
|
+
end
|
123
114
|
|
124
|
-
|
125
|
-
|
126
|
-
|
115
|
+
def contains_any?(other)
|
116
|
+
!contains_none?(other)
|
117
|
+
end
|
127
118
|
|
128
|
-
|
129
|
-
|
130
|
-
|
119
|
+
def contains_none?(other)
|
120
|
+
(other & self).empty?
|
121
|
+
end
|
131
122
|
|
132
|
-
|
133
|
-
|
134
|
-
|
123
|
+
def delete_first
|
124
|
+
self[1..-1]
|
125
|
+
end
|
135
126
|
|
136
|
-
|
137
|
-
|
138
|
-
|
127
|
+
def delete_first!
|
128
|
+
replace(delete_first)
|
129
|
+
end
|
139
130
|
|
140
|
-
|
141
|
-
|
142
|
-
|
131
|
+
def delete_last
|
132
|
+
self[0...-1]
|
133
|
+
end
|
143
134
|
|
144
|
-
|
145
|
-
|
146
|
-
|
135
|
+
def delete_last!
|
136
|
+
replace(delete_last)
|
137
|
+
end
|
147
138
|
|
148
|
-
|
149
|
-
|
150
|
-
|
139
|
+
def delete_values(*args)
|
140
|
+
args.each_with_object([]) { |val, array| array << delete(val) }
|
141
|
+
end
|
151
142
|
|
152
|
-
|
153
|
-
|
154
|
-
|
143
|
+
def demote(value)
|
144
|
+
sort_by { |val| val == value ? 0 : -1 }
|
145
|
+
end
|
155
146
|
|
156
|
-
|
157
|
-
|
158
|
-
|
147
|
+
def demote!(value)
|
148
|
+
replace(demote(value))
|
149
|
+
end
|
159
150
|
|
160
|
-
|
161
|
-
|
162
|
-
|
151
|
+
def denillify(identity = 0)
|
152
|
+
map { |val| val || identity }
|
153
|
+
end
|
163
154
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
hash.delete_if { |_, val| val < minimum }.keys
|
168
|
-
end
|
155
|
+
def denillify!(identity = 0)
|
156
|
+
replace(denillify(identity))
|
157
|
+
end
|
169
158
|
|
170
|
-
|
171
|
-
|
172
|
-
|
159
|
+
def divergence(other)
|
160
|
+
(self - other) | (other - self)
|
161
|
+
end
|
173
162
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
163
|
+
def duplicates(minimum = 2)
|
164
|
+
hash = Hash.new(0)
|
165
|
+
each { |val| hash[val] += 1 }
|
166
|
+
hash.delete_if { |_, val| val < minimum }.keys
|
167
|
+
end
|
178
168
|
|
179
|
-
|
180
|
-
|
169
|
+
def except(*values)
|
170
|
+
reject { |val| values.include?(val) }
|
171
|
+
end
|
181
172
|
|
182
|
-
|
183
|
-
|
173
|
+
def except!(*values)
|
174
|
+
reject! { |val| values.include?(val) }
|
175
|
+
self
|
176
|
+
end
|
184
177
|
|
185
|
-
|
186
|
-
|
178
|
+
def fulfill(value, amount)
|
179
|
+
return self if amount <= size
|
187
180
|
|
188
|
-
|
189
|
-
|
190
|
-
return collection unless rem.positive?
|
181
|
+
fill(value, size..(amount - 1))
|
182
|
+
end
|
191
183
|
|
192
|
-
|
193
|
-
|
184
|
+
def groups(number)
|
185
|
+
return [] if number <= 0
|
194
186
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
array
|
199
|
-
end
|
187
|
+
num, rem = size.divmod(number)
|
188
|
+
collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
|
189
|
+
return collection unless rem.positive?
|
200
190
|
|
201
|
-
|
202
|
-
|
203
|
-
end
|
191
|
+
collection << self[-rem, rem]
|
192
|
+
end
|
204
193
|
|
205
|
-
|
206
|
-
|
207
|
-
|
194
|
+
def indexes(value)
|
195
|
+
array = []
|
196
|
+
each_with_index { |val, i| array << i if value == val }
|
197
|
+
array
|
198
|
+
end
|
208
199
|
|
209
|
-
|
210
|
-
|
211
|
-
|
200
|
+
def match(value)
|
201
|
+
find { |val| value == val }
|
202
|
+
end
|
212
203
|
|
213
|
-
|
214
|
-
|
215
|
-
|
204
|
+
def merge(*values)
|
205
|
+
dup.merge!(*values)
|
206
|
+
end
|
216
207
|
|
217
|
-
|
218
|
-
|
219
|
-
|
208
|
+
def merge!(*values)
|
209
|
+
values.each_with_object(self) { |val, arr| arr.concat(val) }
|
210
|
+
end
|
220
211
|
|
221
|
-
|
222
|
-
|
223
|
-
|
212
|
+
def nillify
|
213
|
+
map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
|
214
|
+
end
|
224
215
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
end
|
216
|
+
def nillify!
|
217
|
+
replace(nillify)
|
218
|
+
end
|
229
219
|
|
230
|
-
|
231
|
-
|
232
|
-
|
220
|
+
def only(*values)
|
221
|
+
select { |val| values.include?(val) }
|
222
|
+
end
|
233
223
|
|
234
|
-
|
235
|
-
|
224
|
+
def only!(*values)
|
225
|
+
select! { |val| values.include?(val) }
|
226
|
+
self
|
227
|
+
end
|
236
228
|
|
237
|
-
|
238
|
-
|
239
|
-
|
229
|
+
def position(value)
|
230
|
+
idx = index(value)
|
231
|
+
return idx if idx.nil?
|
240
232
|
|
241
|
-
|
242
|
-
|
243
|
-
differ = 0.0
|
233
|
+
idx + 1
|
234
|
+
end
|
244
235
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
end
|
236
|
+
def positions(value)
|
237
|
+
indexes(value).map { |val| val + 1 }
|
238
|
+
end
|
249
239
|
|
250
|
-
|
251
|
-
|
252
|
-
|
240
|
+
def probability
|
241
|
+
hash = Hash.new(0.0)
|
242
|
+
differ = 0.0
|
253
243
|
|
254
|
-
|
255
|
-
|
244
|
+
each do |val|
|
245
|
+
hash[val] += 1.0
|
246
|
+
differ += 1.0
|
256
247
|
end
|
257
248
|
|
258
|
-
|
259
|
-
|
260
|
-
|
249
|
+
hash.each_key { |val| hash[val] /= differ }
|
250
|
+
hash
|
251
|
+
end
|
261
252
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
end
|
253
|
+
def promote(value)
|
254
|
+
sort_by { |val| val == value ? -1 : 0 }
|
255
|
+
end
|
266
256
|
|
267
|
-
|
268
|
-
|
269
|
-
|
257
|
+
def promote!(value)
|
258
|
+
sort_by! { |val| val == value ? -1 : 0 }
|
259
|
+
end
|
270
260
|
|
271
|
-
|
272
|
-
|
261
|
+
def rand_sample(max = nil)
|
262
|
+
amount = rand(1..(max || size))
|
263
|
+
sample(amount)
|
264
|
+
end
|
273
265
|
|
274
|
-
|
275
|
-
|
276
|
-
|
266
|
+
def rposition(value)
|
267
|
+
idx = rindex(value)
|
268
|
+
return idx if idx.nil?
|
277
269
|
|
278
|
-
|
279
|
-
|
280
|
-
end
|
270
|
+
idx + 1
|
271
|
+
end
|
281
272
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
end
|
273
|
+
def sample!
|
274
|
+
delete_at(Random.rand(size - 1))
|
275
|
+
end
|
286
276
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
end
|
277
|
+
def strip
|
278
|
+
reject(&:blank?)
|
279
|
+
end
|
291
280
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
alias select_values! only!
|
281
|
+
def strip!
|
282
|
+
reject!(&:blank?)
|
283
|
+
self
|
284
|
+
end
|
297
285
|
|
286
|
+
def swap(from, to)
|
287
|
+
self[from], self[to] = self[to], self[from]
|
288
|
+
self
|
298
289
|
end
|
299
290
|
|
300
|
-
|
291
|
+
alias indices indexes
|
292
|
+
alias reject_values except
|
293
|
+
alias reject_values! except!
|
294
|
+
alias select_values only
|
295
|
+
alias select_values! only!
|
296
|
+
|
301
297
|
end
|
298
|
+
|
299
|
+
require 'lite/ruby/safe/array' unless defined?(ActiveSupport)
|