array_enumerator 0.0.8 → 0.0.9

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
  SHA1:
3
- metadata.gz: ace6e8f8072161752789e9632d5ac47794e84cab
4
- data.tar.gz: dea15db5c0003fb2ecf6041751228726972b8684
3
+ metadata.gz: 886b0a972c29f5806d9c556cb9c212b4cd5e107a
4
+ data.tar.gz: 9bbd84df8ad76af18fca06f1c1c2278fd672dd3e
5
5
  SHA512:
6
- metadata.gz: b7e4d4eb9e0238c68a82b8f6f633b854da6e1d2f28558479b2719275787ee1315d79fe5b84da39441404286858b3d6b9dfbd17a5b04eef119ef970772bba3262
7
- data.tar.gz: 5322459161dc5e2b946c0bd0884dbdd4c35742d3067f2c717a54d3126964789d526790a4be8e97a795c5cf7356d5dad3168b4f4209e5e6e92c661734bc1facc7
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
- results_array = a_enum.select { |element| element.something? }
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.8
1
+ 0.0.9
@@ -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.8 ruby lib
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.8"
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"]
@@ -134,14 +134,24 @@ class ArrayEnumerator
134
134
  end
135
135
 
136
136
  def select
137
- result = []
138
-
139
- check_corrupted
140
- self.each do |element|
141
- result << element if yield(element)
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
- return result
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 = [] if !@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 = [] unless @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
- while ele = @enum.next
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 = [] if !@cache
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 = [] if !@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
- enum = ArrayEnumerator.new do |y|
138
- 10.times do |count|
139
- y << count
140
- end
141
- end
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
- result = enum.select { |element| element == 5}
144
- result.should eq [5]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_enumerator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen