| Class | SyncEnumerator |
| In: |
lib/generator.rb
|
| Parent: | Object |
SyncEnumerator creates an Enumerable object from multiple Enumerable objects and enumerates them synchronously.
require 'generator'
s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
# Yields [1, 'a'], [2, 'b'], and [3,'c']
s.each { |row| puts row.join(', ') }
Creates a new SyncEnumerator which enumerates rows of given Enumerable objects.
# File lib/generator.rb, line 186
186: def initialize(*enums)
187: @gens = enums.map { |e| Generator.new(e) }
188: end
Enumerates rows of the Enumerable objects.
# File lib/generator.rb, line 214
214: def each
215: @gens.each { |g| g.rewind }
216:
217: loop do
218: count = 0
219:
220: ret = @gens.map { |g|
221: if g.end?
222: count += 1
223: nil
224: else
225: g.next
226: end
227: }
228:
229: if count == @gens.size
230: break
231: end
232:
233: yield ret
234: end
235:
236: self
237: end
Returns true if the given nth Enumerable object has reached the end. If no argument is given, returns true if any of the Enumerable objects has reached the end.
# File lib/generator.rb, line 205
205: def end?(i = nil)
206: if i.nil?
207: @gens.detect { |g| g.end? } ? true : false
208: else
209: @gens[i].end?
210: end
211: end
Returns the number of enumerated Enumerable objects, i.e. the size of each row.
# File lib/generator.rb, line 198
198: def length
199: @gens.length
200: end
Returns the number of enumerated Enumerable objects, i.e. the size of each row.
# File lib/generator.rb, line 192
192: def size
193: @gens.size
194: end