# http://liufengyun.chaos-lab.com/prog/2013/10/23/continuation-in-ruby.html require "continuation" # Here, continuation allows f to call back into g after g returned; # With it, g acts like a "generator" for traversing the array. # Note that the array is "private" to g; it only exists in g's stack # frame and the snapshots of the program state that continuations take # when created by callcc. Yet you can iterate through it! def g arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] cc = callcc {|cc| cc} puts arr.shift return cc, arr.size end def f c, size = g c.call(c) if size > 0 end f