# From http://liufengyun.chaos-lab.com/prog/2013/10/23/continuation-in-ruby.html require "continuation" # Here, cc calls back into f from g, and causes g to be # called again and again (until the array is empty). # # This looks a bit like cooperative threading, with functions # giving each other control and/or tasks. Ruby's "yield" # can be implemented this way. def f arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] cc = callcc {|cc| cc} g(cc, arr.shift) if arr.size > 0 end def g(cont, message) puts message cont.call(cont) end f