Excerpts from Pit Capitain's mail of 10 Feb 2005 (EST):
> you can find examples for continuations and coroutines in the archives
> of ruby-talk. You can also look at the Generator class in the standard
> library. I think for jumping back and forth you need two continuations
> instead of just one continuation and a throw/catch. I can dig out some
> code samples if you need them.

Pit is absolutely right: you don't need throw/catch, just two
continuations that you jump back and forth between.

Here's something I whipped up that lets you suspend and resume an
arbitrary function. For simplicity, it doesn't pass return values from
resume/suspend, but it wouldn't be too hard to modify it to do that.

(I also wrote about how generator.rb works at
http://www.all-thing.net/Ruby/iterators_generators_and_continuations_in_ruby.html, which describes some of the idioms in using continuations.)

class Resumable
  def initialize(&proc)
    @inside = @outside = nil
    @proc = proc
  end

  def call(*a)
    raise "not in callable state" if @inside
    if @outside = callcc { |c| c }
      @proc.call self, *a
      @outside.call if @outside
    end
  end

  def resume
    raise "not in a resumable state" unless @inside
    @inside.call if @outside = callcc { |c| c }
  end

  def suspend
    raise "not in a suspendable state" unless @outside
    @outside.call if @inside = callcc { |c| c }
  end
end

## example usage:

f = Resumable.new do |r|
  puts "started!"
  r.suspend
  puts "in the middle"
  r.suspend
  puts "done!"
end

i = 0
puts "calling, i = #{i}"
f.call
puts "suspended, i = #{i}"

i += 1
puts "resuming, i = #{i}"
f.resume
puts "suspended, i = #{i}"

i += 1
puts "resuming, i = #{i}"
f.resume
puts "done, i = #{i}"

## end

HTH,

-- 
William <wmorgan-ruby-talk / masanjin.net>