array_enumerator 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/VERSION +1 -1
- data/lib/array_enumerator.rb +12 -0
- data/spec/array_enumerator_spec.rb +34 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d86472c1fa158b7a9188e0858ff624e3d11b4e54
|
4
|
+
data.tar.gz: 2533a69707036ee1b7bb35dd16ef82d79e68292a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f81050595e5c143a22ed32a2380e84d3eb256abed8ab9ac72b16fb9aeb13a601c4ae30394f6fef0bb8a2008ff0c59ea2c80b7c3986e3fdebfd1273ddbe11ecc
|
7
|
+
data.tar.gz: 0074841d43c186559c64bc06f7e1310c29619050436ba36bfceb4c1b2099ad16b120429d3a929d3c9716cb7b22121c0192c38f96fc73f9595e3650af82a603d5
|
data/README.md
CHANGED
@@ -24,6 +24,17 @@ a_enum = ArrayEnumerator.new do |y|
|
|
24
24
|
end
|
25
25
|
```
|
26
26
|
|
27
|
+
Or give it a normal Enumerator that already exists:
|
28
|
+
```ruby
|
29
|
+
enum = Enumerator.new do |y|
|
30
|
+
1_000.times do |count|
|
31
|
+
y << count
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
a_enum = ArrayEnumerator.new(enum)
|
36
|
+
```
|
37
|
+
|
27
38
|
Call array-methods like you normally would:
|
28
39
|
```ruby
|
29
40
|
a_enum.empty? #=> false
|
@@ -32,6 +43,17 @@ a_enum.shift #=> 2
|
|
32
43
|
a_enum[2] #=> 3
|
33
44
|
a_enum.each_index { |count| puts "Count: #{count}" }
|
34
45
|
a_enum.length #=> 3
|
46
|
+
results_array = a_enum.select { |element| element.something? }
|
47
|
+
```
|
48
|
+
|
49
|
+
### Collect
|
50
|
+
|
51
|
+
Prints out 100, 101 102 etc.
|
52
|
+
```ruby
|
53
|
+
collected_a_enum = a_enum.collect { |count| count + 100 }
|
54
|
+
collected_a_enum.each do |count|
|
55
|
+
print "#{count} "
|
56
|
+
end
|
35
57
|
```
|
36
58
|
|
37
59
|
## Contributing to ArrayEnumerator
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/array_enumerator.rb
CHANGED
@@ -182,6 +182,18 @@ class ArrayEnumerator
|
|
182
182
|
return res
|
183
183
|
end
|
184
184
|
|
185
|
+
def collect
|
186
|
+
check_corrupted
|
187
|
+
|
188
|
+
return ArrayEnumerator.new do |y|
|
189
|
+
self.each do |element|
|
190
|
+
y << yield(element)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
alias map collect
|
196
|
+
|
185
197
|
# Returns a normal array with all elements. Can also raise corrupted error if elements have been thrown out.
|
186
198
|
def to_a
|
187
199
|
check_corrupted
|
@@ -1,6 +1,14 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe "ArrayEnumerator" do
|
4
|
+
let(:a_enum_10) do
|
5
|
+
ArrayEnumerator.new do |y|
|
6
|
+
10.times do |count|
|
7
|
+
y << count
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
4
12
|
it "can be initialized as normal enumerator" do
|
5
13
|
enum = ArrayEnumerator.new do |y|
|
6
14
|
3.times do |count|
|
@@ -131,4 +139,30 @@ describe "ArrayEnumerator" do
|
|
131
139
|
result = enum.select { |element| element == 5}
|
132
140
|
result.should eq [5]
|
133
141
|
end
|
142
|
+
|
143
|
+
describe "#collect" do
|
144
|
+
it "should return a new enumerator yielding the new values one by one" do
|
145
|
+
collected_a_enum = a_enum_10.collect { |element| element + 1000 }
|
146
|
+
|
147
|
+
count = 0
|
148
|
+
collected_a_enum.each do |number|
|
149
|
+
number.should eq (count + 1000)
|
150
|
+
count += 1
|
151
|
+
end
|
152
|
+
|
153
|
+
count.should eq 10
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should work with map and block-symbols" do
|
157
|
+
collected_a_enum = a_enum_10.map(&:to_f)
|
158
|
+
|
159
|
+
count = 0
|
160
|
+
collected_a_enum.each do |number|
|
161
|
+
number.should eq count.to_f
|
162
|
+
count += 1
|
163
|
+
end
|
164
|
+
|
165
|
+
count.should eq 10
|
166
|
+
end
|
167
|
+
end
|
134
168
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kasper Johansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|