⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

{{library}} [[Ruby]] 1.9 moved the method Kernel#callcc and the class Continuation from the core language to a standard library. Starting with Ruby 1.9, programs that use these features must require 'continuation'.

# This code works with both Ruby 1.8 and Ruby 1.9.
require 'continuation' unless defined? Continuation

Most Ruby programs will never use this library.

[[MRI]] has a slow implementation of continuations.

Continuations make spaghetti code with very confusing control flow.

Kernel#callcc creates a continuation. Continuation#call, also known as Continuation#[], continues the program from the place that called Kernel#callcc. With a continuation, you can continue a function call after it ends.

def f
  puts "1st line of output"
  callcc { |cc| return cc }  # f ends with a return...
  puts "3rd line of output"
  return nil

cont = f
if cont
  puts "2nd line of output"
  cont.call                  # ...but this continues f

Kernel#callcc from Ruby is like call-with-current-continuation from [[Scheme]], and almost like setjmp from [[C]], except that setjmp saves less information. (With setjmp, you must not continue a function call after it ends, because C frees the stack frame when it ends.)