callccでコルーチン

呼び出し元のスタックフレームまで記憶しているのがワケワカ。
継続とコルーチンは違うのね…

def coroutine
  loop do
    puts ''
    continue = callcc {|c| c } and return(continue)
    puts ''
    continue = callcc {|c| c } and return(continue)
    puts ''
    continue = callcc {|c| c } and return(continue)
    puts ''
    continue = callcc {|c| c } and return(continue)
  end
end

while (c = coroutine)
  gets
  c.call
end