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.

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

current   each   end?   index   new   next   next?   pos   rewind   yield  

Included Modules

Enumerable

Public Class methods

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.

[Source]

    # 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

Public Instance methods

Returns the element at the current position.

[Source]

     # File lib/generator.rb, line 141
141:   def current()
142:     if @queue.empty?
143:       raise EOFError, "no more elements available"
144:     end
145: 
146:     @queue.first
147:   end

Rewinds the generator and enumerates the elements.

[Source]

     # 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.

[Source]

     # 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 current index (position) counting from zero.

[Source]

     # File lib/generator.rb, line 116
116:   def index()
117:     @index
118:   end

Returns the element at the current position and moves forward.

[Source]

     # 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.

[Source]

     # File lib/generator.rb, line 111
111:   def next?()
112:     !end?
113:   end

Returns the current index (position) counting from zero.

[Source]

     # File lib/generator.rb, line 121
121:   def pos()
122:     @index
123:   end

Rewinds the generator.

[Source]

     # File lib/generator.rb, line 150
150:   def rewind()
151:     initialize(nil, &@block) if @index.nonzero?
152: 
153:     self
154:   end

Yields an element to the generator.

[Source]

    # File lib/generator.rb, line 92
92:   def yield(value)
93:     if @cont_yield = callcc { |c| c }
94:       @queue << value
95:       @cont_next.call(nil)
96:     end
97: 
98:     self
99:   end

[Validate]