class Generator
  def initialize
    do_generation
  end
  def next
    callcc do |here|
      @main_context = here;
      @generator_context.call
    end
  end
  private
  def do_generation
    callcc do |context|
      @generator_context = context;
      return
    end
    generating_loop
  end
 do |context|
       @generator_context = context;
       @main_context.call(value)
     end
  end
end

# Subclass this and define a generating_loop

class FibGenerator < Generator
  def generating_loop
     generate(1)
     a, b = 1, 1
     loop do
       generate(b)
       a, b = b, a+b
     end
  end
end

# Now instantiate the class...

fib = FibGenerator.new
puts  fib.next            # 1
puts  fib.next            # 1
puts  fib.next            # 2
puts  fib.next            # 3
puts  fib.next            # 5
puts  fib.next            # 8
puts  fib.next            # 13
# And so on...

