Continuation

From RubySpec

Jump to: navigation, search

From ruby-doc, "Continuation objects are generated by Kernel#callcc. They hold a return address and execution context, allowing a nonlocal return to the end of the callcc block from anywhere within a program. Continuations are somewhat analogous to a structured version of C‘s setjmp/longjmp (although they contain more state, so you might consider them closer to threads)."

Here is an example similar to that at ruby-doc:

#!/usr/bin/env ruby -w
ary = [0,1,2,3]
callcc {|$cc|}
element = ary.inject(5+(ary.pop)){|j,k| j+k}
puts element
$cc.call unless ary.length == 1

produces

11
8
6
Personal tools