lite-ruby 1.3.3 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 191d0fb7e901d29b7faa905a312fee5928439267a5f30d3e729ba76c5545de98
4
- data.tar.gz: 1549c4736eeed76d892858775d32cd3914c6baed19d25b0bd4becc164d9cfc60
3
+ metadata.gz: 29e7241b666924e764cb6109751dde2dd55b5964a1a5ee1656e20d44373ab516
4
+ data.tar.gz: 875f644d9a7e9bc392b093c0e9d030321e1a1d3230dbd9214f3225df4cdff613
5
5
  SHA512:
6
- metadata.gz: bd7fb280764490cbfd9a581ae64d8985040bb49410169fd8b6c07e048dda78b995eb618155941f67e0ddfe32313def11ce8e3b86971f1564824d7de1c3177579
7
- data.tar.gz: b1981a0e40ffcb31ad5302ace5ecc094e548415fc7e128edc873a2d7b5797450a4c7d6bdfee0e363f90041a0a362f90bb602fa2a4c1085f29700cc6a33948ff4
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 (1.3.3)
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.10.0)
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.5.3)
47
+ mini_portile2 (2.6.1)
48
48
  minitest (5.14.4)
49
- nokogiri (1.11.7)
50
- mini_portile2 (~> 2.5.0)
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.3)
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.7.0, < 2.0)
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.16.0)
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
- Lite::Ruby.configure do |config|
41
- config.monkey_patches = %w[
42
- array boolean date enumerable hash integer kernel numeric object open_struct range string
43
- struct time
44
- ]
45
- end
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
- `to_nearest value`
357
+ `to_nearest_value`
321
358
  ------
322
- return the value in values that is nearest to the number.
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
- Lite::Ruby.configure do |config|
4
- config.monkey_patches = %w[
5
- array boolean date enumerable hash integer kernel numeric object open_struct range string
6
- struct time
7
- ]
8
- end
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
- %w[version configuration].each do |filename|
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'
@@ -1,301 +1,299 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('array')
4
- class Array
3
+ class Array
5
4
 
6
- def assert_min_values!(*valid_values)
7
- return self if empty?
5
+ def assert_min_values!(*valid_values)
6
+ return self if empty?
8
7
 
9
- valid_values.each do |value|
10
- next if include?(value)
8
+ valid_values.each do |value|
9
+ next if include?(value)
11
10
 
12
- raise ArgumentError,
13
- "Missing value: #{value.inspect}. " \
14
- "Minimum values are: #{valid_values.map(&:inspect).join(', ')}"
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
- def assert_all_min_values!(*valid_values)
21
- return assert_min_values!(*valid_values) unless empty?
16
+ self
17
+ end
22
18
 
23
- raise ArgumentError, 'An empty array is not allowed'
24
- end
19
+ def assert_all_min_values!(*valid_values)
20
+ return assert_min_values!(*valid_values) unless empty?
25
21
 
26
- def assert_valid_values!(*valid_values)
27
- each do |value|
28
- next if valid_values.include?(value)
22
+ raise ArgumentError, 'An empty array is not allowed'
23
+ end
29
24
 
30
- raise ArgumentError,
31
- "Invalid value: #{value.inspect}. " \
32
- "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
33
- end
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
- def assert_all_valid_values!(*valid_values)
37
- return assert_valid_values!(*valid_values) unless empty?
35
+ def assert_all_valid_values!(*valid_values)
36
+ return assert_valid_values!(*valid_values) unless empty?
38
37
 
39
- raise ArgumentError, 'An empty array is not allowed'
40
- end
38
+ raise ArgumentError, 'An empty array is not allowed'
39
+ end
41
40
 
42
- def assert_value_presence!
43
- each do |value|
44
- next if value.respond_to?(:present?) ? value.present? : value
41
+ def assert_value_presence!
42
+ each do |value|
43
+ next if value.respond_to?(:present?) ? value.present? : value
45
44
 
46
- raise ArgumentError, "A #{value.inspect} value is not allowed"
47
- end
45
+ raise ArgumentError, "A #{value.inspect} value is not allowed"
48
46
  end
47
+ end
49
48
 
50
- def assert_all_value_presence!
51
- return assert_value_presence! unless empty?
49
+ def assert_all_value_presence!
50
+ return assert_value_presence! unless empty?
52
51
 
53
- raise ArgumentError, 'An empty array is not allowed'
54
- end
52
+ raise ArgumentError, 'An empty array is not allowed'
53
+ end
55
54
 
56
- def after(value)
57
- return unless include?(value)
55
+ def after(value)
56
+ return unless include?(value)
58
57
 
59
- self[(index(value) + 1) % size]
60
- end
58
+ self[(index(value) + 1) % size]
59
+ end
61
60
 
62
- def all_after(value)
63
- return unless include?(value)
61
+ def all_after(value)
62
+ return unless include?(value)
64
63
 
65
- i = index(value)
66
- return if i == (size - 1)
64
+ i = index(value)
65
+ return if i == (size - 1)
67
66
 
68
- self[(i + 1)..-1]
69
- end
67
+ self[(i + 1)..-1]
68
+ end
70
69
 
71
- def all_before(value)
72
- return unless include?(value)
70
+ def all_before(value)
71
+ return unless include?(value)
73
72
 
74
- i = index(value)
75
- return if i.zero?
73
+ i = index(value)
74
+ return if i.zero?
76
75
 
77
- self[0..(i - 1)]
78
- end
76
+ self[0..(i - 1)]
77
+ end
79
78
 
80
- def before(value)
81
- return unless include?(value)
79
+ def before(value)
80
+ return unless include?(value)
82
81
 
83
- self[(index(value) - 1) % size]
84
- end
82
+ self[(index(value) - 1) % size]
83
+ end
85
84
 
86
- # rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
87
- # rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
88
- def bury(*args)
89
- if args.count < 2
90
- raise ArgumentError, '2 or more arguments required'
91
- elsif args.count == 2
92
- if args[0].is_a?(Integer)
93
- self[args[0]] = args[1]
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
- if args[0].is_a?(Integer)
99
- arg = args.shift
100
- self[arg] = [] unless self[arg]
101
- self[arg].bury(*args)
102
- else
103
- self << {}.bury(*args)
104
- end
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
- def contains_any?(other)
117
- !contains_none?(other)
118
- end
106
+ self
107
+ end
108
+ # rubocop:enable Style/GuardClause, Style/IfInsideElse
109
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
119
110
 
120
- def contains_none?(other)
121
- (other & self).empty?
122
- end
111
+ def contains_all?(other)
112
+ (other & self) == self
113
+ end
123
114
 
124
- def delete_first
125
- self[1..-1]
126
- end
115
+ def contains_any?(other)
116
+ !contains_none?(other)
117
+ end
127
118
 
128
- def delete_first!
129
- replace(delete_first)
130
- end
119
+ def contains_none?(other)
120
+ (other & self).empty?
121
+ end
131
122
 
132
- def delete_last
133
- self[0...-1]
134
- end
123
+ def delete_first
124
+ self[1..-1]
125
+ end
135
126
 
136
- def delete_last!
137
- replace(delete_last)
138
- end
127
+ def delete_first!
128
+ replace(delete_first)
129
+ end
139
130
 
140
- def delete_values(*args)
141
- args.each_with_object([]) { |val, array| array << delete(val) }
142
- end
131
+ def delete_last
132
+ self[0...-1]
133
+ end
143
134
 
144
- def demote(value)
145
- sort_by { |val| val == value ? 0 : -1 }
146
- end
135
+ def delete_last!
136
+ replace(delete_last)
137
+ end
147
138
 
148
- def demote!(value)
149
- replace(demote(value))
150
- end
139
+ def delete_values(*args)
140
+ args.each_with_object([]) { |val, array| array << delete(val) }
141
+ end
151
142
 
152
- def denillify(identity = 0)
153
- map { |val| val || identity }
154
- end
143
+ def demote(value)
144
+ sort_by { |val| val == value ? 0 : -1 }
145
+ end
155
146
 
156
- def denillify!(identity = 0)
157
- replace(denillify(identity))
158
- end
147
+ def demote!(value)
148
+ replace(demote(value))
149
+ end
159
150
 
160
- def divergence(other)
161
- (self - other) | (other - self)
162
- end
151
+ def denillify(identity = 0)
152
+ map { |val| val || identity }
153
+ end
163
154
 
164
- def duplicates(minimum = 2)
165
- hash = Hash.new(0)
166
- each { |val| hash[val] += 1 }
167
- hash.delete_if { |_, val| val < minimum }.keys
168
- end
155
+ def denillify!(identity = 0)
156
+ replace(denillify(identity))
157
+ end
169
158
 
170
- def except(*values)
171
- reject { |val| values.include?(val) }
172
- end
159
+ def divergence(other)
160
+ (self - other) | (other - self)
161
+ end
173
162
 
174
- def except!(*values)
175
- reject! { |val| values.include?(val) }
176
- self
177
- end
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
- def fulfill(value, amount)
180
- return self if amount <= size
169
+ def except(*values)
170
+ reject { |val| values.include?(val) }
171
+ end
181
172
 
182
- fill(value, size..(amount - 1))
183
- end
173
+ def except!(*values)
174
+ reject! { |val| values.include?(val) }
175
+ self
176
+ end
184
177
 
185
- def groups(number)
186
- return [] if number <= 0
178
+ def fulfill(value, amount)
179
+ return self if amount <= size
187
180
 
188
- num, rem = size.divmod(number)
189
- collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
190
- return collection unless rem.positive?
181
+ fill(value, size..(amount - 1))
182
+ end
191
183
 
192
- collection << self[-rem, rem]
193
- end
184
+ def groups(number)
185
+ return [] if number <= 0
194
186
 
195
- def indexes(value)
196
- array = []
197
- each_with_index { |val, i| array << i if value == val }
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
- def match(value)
202
- find { |val| value == val }
203
- end
191
+ collection << self[-rem, rem]
192
+ end
204
193
 
205
- def merge(*values)
206
- dup.merge!(*values)
207
- end
194
+ def indexes(value)
195
+ array = []
196
+ each_with_index { |val, i| array << i if value == val }
197
+ array
198
+ end
208
199
 
209
- def merge!(*values)
210
- values.each_with_object(self) { |val, arr| arr.concat(val) }
211
- end
200
+ def match(value)
201
+ find { |val| value == val }
202
+ end
212
203
 
213
- def nillify
214
- map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
215
- end
204
+ def merge(*values)
205
+ dup.merge!(*values)
206
+ end
216
207
 
217
- def nillify!
218
- replace(nillify)
219
- end
208
+ def merge!(*values)
209
+ values.each_with_object(self) { |val, arr| arr.concat(val) }
210
+ end
220
211
 
221
- def only(*values)
222
- select { |val| values.include?(val) }
223
- end
212
+ def nillify
213
+ map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
214
+ end
224
215
 
225
- def only!(*values)
226
- select! { |val| values.include?(val) }
227
- self
228
- end
216
+ def nillify!
217
+ replace(nillify)
218
+ end
229
219
 
230
- def position(value)
231
- idx = index(value)
232
- return idx if idx.nil?
220
+ def only(*values)
221
+ select { |val| values.include?(val) }
222
+ end
233
223
 
234
- idx + 1
235
- end
224
+ def only!(*values)
225
+ select! { |val| values.include?(val) }
226
+ self
227
+ end
236
228
 
237
- def positions(value)
238
- indexes(value).map { |val| val + 1 }
239
- end
229
+ def position(value)
230
+ idx = index(value)
231
+ return idx if idx.nil?
240
232
 
241
- def probability
242
- hash = Hash.new(0.0)
243
- differ = 0.0
233
+ idx + 1
234
+ end
244
235
 
245
- each do |val|
246
- hash[val] += 1.0
247
- differ += 1.0
248
- end
236
+ def positions(value)
237
+ indexes(value).map { |val| val + 1 }
238
+ end
249
239
 
250
- hash.each_key { |val| hash[val] /= differ }
251
- hash
252
- end
240
+ def probability
241
+ hash = Hash.new(0.0)
242
+ differ = 0.0
253
243
 
254
- def promote(value)
255
- sort_by { |val| val == value ? -1 : 0 }
244
+ each do |val|
245
+ hash[val] += 1.0
246
+ differ += 1.0
256
247
  end
257
248
 
258
- def promote!(value)
259
- sort_by! { |val| val == value ? -1 : 0 }
260
- end
249
+ hash.each_key { |val| hash[val] /= differ }
250
+ hash
251
+ end
261
252
 
262
- def rand_sample(max = nil)
263
- amount = rand(1..(max || size))
264
- sample(amount)
265
- end
253
+ def promote(value)
254
+ sort_by { |val| val == value ? -1 : 0 }
255
+ end
266
256
 
267
- def rposition(value)
268
- idx = rindex(value)
269
- return idx if idx.nil?
257
+ def promote!(value)
258
+ sort_by! { |val| val == value ? -1 : 0 }
259
+ end
270
260
 
271
- idx + 1
272
- end
261
+ def rand_sample(max = nil)
262
+ amount = rand(1..(max || size))
263
+ sample(amount)
264
+ end
273
265
 
274
- def sample!
275
- delete_at(Random.rand(size - 1))
276
- end
266
+ def rposition(value)
267
+ idx = rindex(value)
268
+ return idx if idx.nil?
277
269
 
278
- def strip
279
- reject(&:blank?)
280
- end
270
+ idx + 1
271
+ end
281
272
 
282
- def strip!
283
- reject!(&:blank?)
284
- self
285
- end
273
+ def sample!
274
+ delete_at(Random.rand(size - 1))
275
+ end
286
276
 
287
- def swap(from, to)
288
- self[from], self[to] = self[to], self[from]
289
- self
290
- end
277
+ def strip
278
+ reject(&:blank?)
279
+ end
291
280
 
292
- alias indices indexes
293
- alias reject_values except
294
- alias reject_values! except!
295
- alias select_values only
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
- require 'lite/ruby/safe/array' unless defined?(ActiveSupport)
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)