| Class | Generator |
| In: |
lib/generator.rb
|
| Parent: | Object |
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.
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
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.
# File lib/generator.rb, line 69
69: def initialize(enum = nil, &block)
70: if enum
71: @block = proc { |g|
72: enum.each { |x| g.yield x }
73: }
74: else
75: @block = block
76: end
77:
78: @index = 0
79: @queue = []
80: @cont_next = @cont_yield = @cont_endp = nil
81:
82: if @cont_next = callcc { |c| c }
83: @block.call(self)
84:
85: @cont_endp.call(nil) if @cont_endp
86: end
87:
88: self
89: end
Rewinds the generator and enumerates the elements.
# File lib/generator.rb, line 157
157: def each
158: rewind
159:
160: until end?
161: yield self.next
162: end
163:
164: self
165: end
Returns true if the generator has reached the end.
# File lib/generator.rb, line 102
102: def end?()
103: if @cont_endp = callcc { |c| c }
104: @cont_yield.nil? && @queue.empty?
105: else
106: @queue.empty?
107: end
108: end
Returns the element at the current position and moves forward.
# File lib/generator.rb, line 126
126: def next()
127: if end?
128: raise EOFError, "no more elements available"
129: end
130:
131: if @cont_next = callcc { |c| c }
132: @cont_yield.call(nil) if @cont_yield
133: end
134:
135: @index += 1
136:
137: @queue.shift
138: end
Returns true if the generator has not reached the end yet.
# File lib/generator.rb, line 111
111: def next?()
112: !end?
113: end
Rewinds the generator.
# File lib/generator.rb, line 150
150: def rewind()
151: initialize(nil, &@block) if @index.nonzero?
152:
153: self
154: end