array_enumerator 0.0.8 → 0.0.9
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/README.md +1 -1
- data/VERSION +1 -1
- data/array_enumerator.gemspec +2 -2
- data/lib/array_enumerator.rb +30 -11
- data/spec/array_enumerator_spec.rb +11 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 886b0a972c29f5806d9c556cb9c212b4cd5e107a
|
4
|
+
data.tar.gz: 9bbd84df8ad76af18fca06f1c1c2278fd672dd3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0027e5400913049582dbd0b53fdb96fd26984fcd64ecb99604be18d2c21689a1f9064861b48543a4bbc9cc27f42c6c26966b8cf00ed51660d090224576e335a7
|
7
|
+
data.tar.gz: 30682aa4a63b7157722db7fc2dfe55615224fb29511570fbef91a767d99c791b3b646be04aea539cf09012d3f17c58969f787730368d8c32c3fe136bb01ace7c
|
data/README.md
CHANGED
@@ -43,7 +43,7 @@ a_enum.shift #=> 2
|
|
43
43
|
a_enum[2] #=> 3
|
44
44
|
a_enum.each_index { |count| puts "Count: #{count}" }
|
45
45
|
a_enum.length #=> 3
|
46
|
-
|
46
|
+
a_enum.select { |element| element.to_f }.to_a #=> [0.0, 1.0, 2.0 etc]
|
47
47
|
```
|
48
48
|
|
49
49
|
### Collect
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/array_enumerator.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: array_enumerator 0.0.
|
5
|
+
# stub: array_enumerator 0.0.9 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "array_enumerator"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.9"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
data/lib/array_enumerator.rb
CHANGED
@@ -134,14 +134,24 @@ class ArrayEnumerator
|
|
134
134
|
end
|
135
135
|
|
136
136
|
def select
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
137
|
+
return ArrayEnumerator.new do |y|
|
138
|
+
check_corrupted
|
139
|
+
self.each do |element|
|
140
|
+
y << element if yield(element)
|
141
|
+
end
|
142
142
|
end
|
143
|
+
end
|
143
144
|
|
144
|
-
|
145
|
+
alias keep_if select
|
146
|
+
|
147
|
+
def reject
|
148
|
+
select { |element| !yield(element) }
|
149
|
+
end
|
150
|
+
|
151
|
+
alias delete_if reject
|
152
|
+
|
153
|
+
def compact
|
154
|
+
reject { |element| element == nil }
|
145
155
|
end
|
146
156
|
|
147
157
|
# Giving slice negaive arguments will force it to cache all elements and crush the memory for big results.
|
@@ -160,7 +170,7 @@ class ArrayEnumerator
|
|
160
170
|
raise "Dont now what to do with args: '#{args}'."
|
161
171
|
end
|
162
172
|
|
163
|
-
@eles
|
173
|
+
@eles ||= []
|
164
174
|
cache_eles = need_eles - @eles.length if need_eles
|
165
175
|
cache_ele(cache_eles) if need_eles && cache_eles > 0
|
166
176
|
return @eles.slice(*args)
|
@@ -176,7 +186,7 @@ class ArrayEnumerator
|
|
176
186
|
amount = 1
|
177
187
|
end
|
178
188
|
|
179
|
-
@eles
|
189
|
+
@eles ||= []
|
180
190
|
cache_ele(amount - @eles.length) if !@eles || @eles.length < amount
|
181
191
|
res = @eles.shift(*args)
|
182
192
|
|
@@ -211,6 +221,12 @@ class ArrayEnumerator
|
|
211
221
|
|
212
222
|
alias to_ary to_a
|
213
223
|
|
224
|
+
def to_s
|
225
|
+
"<ArrayEnumerator array_corrupted=\"#{@array_corrupted}\" length_cache=\"#{@length_cache}\">"
|
226
|
+
end
|
227
|
+
|
228
|
+
alias inspect to_s
|
229
|
+
|
214
230
|
private
|
215
231
|
|
216
232
|
# Raises error because elements have been forgotten to spare memory.
|
@@ -224,10 +240,13 @@ private
|
|
224
240
|
|
225
241
|
begin
|
226
242
|
@mutex.synchronize do
|
227
|
-
|
243
|
+
loop do
|
244
|
+
ele = @enum.next
|
228
245
|
@length_cache += 1
|
229
246
|
yield(ele)
|
230
247
|
end
|
248
|
+
|
249
|
+
@end = true # How it actually breaks is beyond me...
|
231
250
|
end
|
232
251
|
rescue StopIteration
|
233
252
|
@end = true
|
@@ -236,7 +255,7 @@ private
|
|
236
255
|
|
237
256
|
# Caches a given amount of elements.
|
238
257
|
def cache_ele(amount = 1)
|
239
|
-
@eles
|
258
|
+
@eles ||= []
|
240
259
|
|
241
260
|
begin
|
242
261
|
@mutex.synchronize do
|
@@ -252,7 +271,7 @@ private
|
|
252
271
|
|
253
272
|
# Forces the rest of the elements to be cached.
|
254
273
|
def cache_all
|
255
|
-
@eles
|
274
|
+
@eles ||= []
|
256
275
|
|
257
276
|
begin
|
258
277
|
@mutex.synchronize do
|
@@ -134,14 +134,18 @@ describe "ArrayEnumerator" do
|
|
134
134
|
end
|
135
135
|
|
136
136
|
it "#select" do
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
137
|
+
result = a_enum_10.select { |element| element == 5 || element == 7 }.to_a
|
138
|
+
result.should eq [5, 7]
|
139
|
+
end
|
140
|
+
|
141
|
+
it "#reject" do
|
142
|
+
result = a_enum_10.reject { |element| element == 5 || element == 7}.to_a
|
143
|
+
result.should eq [0, 1, 2, 3, 4, 6, 8, 9]
|
144
|
+
end
|
142
145
|
|
143
|
-
|
144
|
-
|
146
|
+
it "#compact" do
|
147
|
+
a_enum = ArrayEnumerator.new([0, nil, 1, 2, 3, 4, nil, 5].to_enum)
|
148
|
+
a_enum.compact.to_a.should eq [0, 1, 2, 3, 4, 5]
|
145
149
|
end
|
146
150
|
|
147
151
|
describe "#collect" do
|