Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • generator.rb

Parent

Included Modules

Class/Module Index [+]

Quicksearch

SyncEnumerator

SyncEnumerator creates an Enumerable object from multiple Enumerable objects and enumerates them synchronously.

Example

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(', ') }

Public Class Methods

new(*enums) click to toggle source

Creates a new SyncEnumerator which enumerates rows of given Enumerable objects.

 
               # File generator.rb, line 224
def initialize(*enums)
  @gens = enums.map { |e| Generator.new(e) }
end
            

Public Instance Methods

each() click to toggle source

Enumerates rows of the Enumerable objects.

 
               # File generator.rb, line 252
def each
  @gens.each { |g| g.rewind }

  loop do
    count = 0

    ret = @gens.map { |g|
      if g.end?
        count += 1
        nil
      else
        g.next
      end
    }

    if count == @gens.size
      break
    end

    yield ret
  end

  self
end
            
end?(i = nil) click to toggle source

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 generator.rb, line 243
def end?(i = nil)
  if i.nil?
    @gens.detect { |g| g.end? } ? true : false
  else
    @gens[i].end?
  end
end
            
length() click to toggle source

Returns the number of enumerated Enumerable objects, i.e. the size of each row.

 
               # File generator.rb, line 236
def length
  @gens.length
end
            
size() click to toggle source

Returns the number of enumerated Enumerable objects, i.e. the size of each row.

 
               # File generator.rb, line 230
def size
  @gens.size
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus