- Inheritance
- < Object
- Included Modules
- Enumerable
Generator converts an internal iterator (i.e. an Enumerable object) to an external iterator.
Note that it is not very fast since it is implemented using continuations, which are currently slow.
Example
require 'generator' # Generator from an Enumerable object g = Generator.new(['A', 'B', 'C', 'Z']) while g.next? puts g.next end # Generator from a block g = Generator.new { |g| for i in 'A'..'C' g.yield i end g.yield 'Z' } # The same result as above while g.next? puts g.next end
Methods
Class
Visibility | Signature |
---|---|
public | new (enum = nil, &block) |
Instance
Visibility | Signature |
---|---|
public | current () |
public | each () {|self.next| ...} |
public | end? () |
public | index () |
public | next () |
public | next? () |
public | pos () |
public | rewind () |
public | yield (value) |
Class Method Detail
new(enum = nil, &block)
Creates a new generator either from an Enumerable object or from a block.
In the former, block is ignored even if given.
In the latter, the given block is called with the generator itself, and expected to call the yield method for each element.
Instance Method Detail
current()
Returns the element at the current position.
each() {|self.next| ...}
Rewinds the generator and enumerates the elements.
end?()
Returns true if the generator has reached the end.
index()
next()
Returns the element at the current position and moves forward.
next?()
Returns true if the generator has not reached the end yet.
pos()
rewind()
Rewinds the generator.
yield(value)
Yields an element to the generator.