On 25 Mar 2003 at 14:25, Hal E. Fulton wrote:

> What I tried to do was encapsulate this 
> functionality in a module called Gen, so
> that I could, for instance, do this:
> 
>   class Array
>     include Gen
>   end
> 
>   x = [5, 6, 7, 8, 9]
> 
>   loop do
>     a = x.get
>     break if x.nil?
>     p a
>   end

Hal,

you could try it with

require 'generator'

module Gen
  def get
    unless @generator
      @generator = Generator.new do |generator|
        each { |value| generator.generate(value) }
      end
    end
    @generator.get
  end
end

Simply create a generator as in Jim's excellent tutorial if necessary 
and then call its get Method.

Or even

require 'generator'

module Enumerable
  def generator
    Generator.new do |gen|
      each { |value| gen.generate(value) }
    end
  end
end

g = [ 5, 6, 7, 8, 9 ].generator

loop do
  a = g.get
  break if a.nil?
  p a
end

If you go this route, you can create multiple independent generators 
of the same Enumerable.

Regards,
Pit