| 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 224
224: def initialize(*enums)
225: @gens = enums.map { |e| Generator.new(e) }
226: end
Enumerates rows of the Enumerable objects.
# File lib/generator.rb, line 252
252: def each
253: @gens.each { |g| g.rewind }
254:
255: loop do
256: count = 0
257:
258: ret = @gens.map { |g|
259: if g.end?
260: count += 1
261: nil
262: else
263: g.next
264: end
265: }
266:
267: if count == @gens.size
268: break
269: end
270:
271: yield ret
272: end
273:
274: self
275: 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 243
243: def end?(i = nil)
244: if i.nil?
245: @gens.detect { |g| g.end? } ? true : false
246: else
247: @gens[i].end?
248: end
249: end
Returns the number of enumerated Enumerable objects, i.e. the size of each row.
# File lib/generator.rb, line 236
236: def length
237: @gens.length
238: end
Returns the number of enumerated Enumerable objects, i.e. the size of each row.
# File lib/generator.rb, line 230
230: def size
231: @gens.size
232: end