# Continuation - where does it continue

**URL:** <https://rubytalk.org/t/continuation-where-does-it-continue/20971>\
**Category:** ruby-talk\
**Created:** [21 September 2005 14:01 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971 "2005-09-21T14:01:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://rubytalk.org/u/michele)\
**Post date:** [21 September 2005 14:01 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/1 "2005-09-21T14:01:39Z")

</div>

Why does the following code print the line "doing other stuff" twice?

def cc  
&nbsp;&nbsp;&nbsp;&nbsp;puts "do A"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 1. #{caller}"  
&nbsp;&nbsp;&nbsp;&nbsp;callcc { |cont| return cont}  
&nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 2. #{caller}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "do B"  
end

puts "calling cc"  
cont = cc()  
puts "doing other stuff"  
cont.call() if cont  
puts "end of work"  
puts

OUTPUT:  
calling cc  
do A  
----\> 1. remove2.rb:59  
doing other stuff  
----\> 2. remove2.rb:59  
do B  
doing other stuff  
end of work

---

<div class="post-metadata">

**Author:** ![Patrick\_Roemer1](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Patrick\_Roemer1](https://rubytalk.org/u/Patrick_Roemer1)\
**Post date:** [21 September 2005 14:16 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/2 "2005-09-21T14:16:39Z")

</div>

Responding to michelemendel@gmail.com:

> Why does the following code print the line "doing other stuff" twice?
> 
> def cc  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "do A"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 1. #{caller}"  
> &nbsp;&nbsp;&nbsp;&nbsp;callcc { |cont| return cont}  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 2. #{caller}"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "do B"  
> end
> 
> puts "calling cc"  
> cont = cc()  
> puts "doing other stuff"  
> cont.call() if cont  
> puts "end of work"  
> puts

The continuation 'continues' from exactly the state where it left off: It starts after the callcc call, after returning from the cc method it continues one 'stack frame'[1] deeper after the cc() call, just as in normal execution - it won't 'jump back' to the code position after the continuation call.

Best regards,  
Patrick

[1] Java lingo, don't know what it's called in Ruby

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [21 September 2005 14:23 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/3 "2005-09-21T14:23:30Z")

</div>

michelemendel@gmail.com schrieb:

> Why does the following code print the line "doing other stuff" twice?
> 
> def cc  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "do A"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 1. #{caller}"  
> &nbsp;&nbsp;&nbsp;&nbsp;callcc { |cont| return cont}

The continuation continues here, after the call to callcc.

> &nbsp;&nbsp;&nbsp;&nbsp;puts "----\> 2. #{caller}" puts "do B"  
> end
> 
> puts "calling cc"  
> cont = cc() # \<------ (X)  
> puts "doing other stuff"  
> cont.call() if cont  
> puts "end of work"  
> puts
> 
> OUTPUT:  
> calling cc  
> do A  
> ----\> 1. remove2.rb:59  
> doing other stuff  
> ----\> 2. remove2.rb:59  
> do B  
> doing other stuff  
> end of work

Above I've shown the place where the continuation continues. So when you call the continuation, you're back in the cc method after the callcc. The program prints the remaining messages of the cc method and returns nil (the result of puts). After the cc method returns, you're back at the place where the method has been called, which the line marked with (X). cont is set to nil, and the program prints "doing other stuff" for the second time.

If you dont' want this, you have to change the code to something like

&nbsp;&nbsp;&nbsp;puts "calling cc"  
&nbsp;&nbsp;&nbsp;cont = cc()  
&nbsp;&nbsp;&nbsp;if cont  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "doing other stuff"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cont.call()  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;puts "end of work"  
&nbsp;&nbsp;&nbsp;puts

Florian Groß has written something to make Continuation handling easier. You should be able to find it in the mailing list archives.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [21 September 2005 14:36 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/4 "2005-09-21T14:36:30Z")

</div>

michelemendel@gmail.com said:

> Why does the following code print the line "doing other stuff" twice?

01 def cc  
02 puts "do A"  
03 puts "----\> 1. #{caller}"  
04 callcc { |cont| return cont}  
05 puts "----\> 2. #{caller}"  
06 puts "do B"  
07 end

> 

08 puts "calling cc"  
09 cont = cc()  
10 puts "doing other stuff"  
11 cont.call() if cont  
12 puts "end of work"  
13 puts

This is the seqence of events (by line number). Comments on what's  
happening are in parenthesis.

08: calling cc  
09 (calling cc)  
02: do A  
03: ----\> 1. remove2.rb:59  
04: (returning the continuation)  
10: doing other stuff  
11: (calling the continuation, so picking up in the middle of cc)  
05: ----\> 2. remove2.rb:59  
06: do B  
07: (returning from cc to the original location cc call site.  
&nbsp;&nbsp;&nbsp;&nbsp;Now we return a nil value)  
10: doing other stuff  
11: (since cc returned a nil the second time, we don't call the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continuation this time)  
12: end of work

> **···**
>
> --  
> -- Jim Weirich jim@weirichhouse.org [http://onestepback.org](http://onestepback.org)  
> -----------------------------------------------------------------  
> "Beware of bugs in the above code; I have only proved it correct,  
> not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

---

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://rubytalk.org/u/michele)\
**Post date:** [21 September 2005 14:56 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/5 "2005-09-21T14:56:39Z")

</div>

AHA - EUREKA!!!

Thanks for your very quick answers! After "studying" continuations for  
a couple of days, reading everthing I could find about, I think I  
finally understand it. Your answers made it all clear.

Michele

---

<div class="post-metadata">

**Author:** ![Randy\_Kramer](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@Randy\_Kramer](https://rubytalk.org/u/Randy_Kramer)\
**Post date:** [21 September 2005 15:38 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/6 "2005-09-21T15:38:30Z")

</div>

Thanks, the line numbers help a lot!

regards,  
Randy Kramer

> **···**
>
> On Wednesday 21 September 2005 10:36 am, Jim Weirich wrote:
> 
> > michelemendel@gmail.com said:  
> > \> Why does the following code print the line "doing other stuff" twice?
> 
> > This is the seqence of events (by line number). Comments on what's  
> > happening are in parenthesis.

---

<div class="post-metadata">

**Author:** ![Roy\_Pardee](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Roy\_Pardee](https://rubytalk.org/u/Roy_Pardee)\
**Post date:** [22 September 2005 02:51 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/7 "2005-09-22T02:51:39Z")

</div>

What I don't understand is why the line:

&nbsp;&nbsp;&nbsp;&nbsp;04 callcc { |cont| return cont}

Doesn't give an 'undefined local method or variable "callcc"' error.  
To my noob eyes that looks like a method call w/an accompanying block.  
Have you got a clue for me?

Thanks!

-Roy

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [22 September 2005 01:47 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/8 "2005-09-22T01:47:40Z")

</div>

Hi,

this finally cleared up Ruby's continuations for me too.

Would this be the effect you're after?  
# code  
def cc  
&nbsp;&nbsp;&nbsp;puts "do A"  
&nbsp;&nbsp;&nbsp;puts "----\> 1. #{caller}"  
&nbsp;&nbsp;&nbsp;cont = callcc { |cont| return cont}  
&nbsp;&nbsp;&nbsp;puts "----\> 2. #{caller}"  
&nbsp;&nbsp;&nbsp;puts "do B"  
end

puts "calling cc"  
if cont = cc()  
&nbsp;&nbsp;puts "doing other stuff"  
&nbsp;&nbsp;cont.call() if cont  
&nbsp;&nbsp;puts "end of work"  
&nbsp;&nbsp;puts  
end

#output  
\_\_END\_\_  
calling cc  
do A  
----\> 1. continuations.rb:10  
doing other stuff  
----\> 2. continuations.rb:10  
do B  
#end

I'm curious - what problem are you trying to solve?

Regards,  
Sean

> **···**
>
> On 9/21/05, michele \<michelemendel@gmail.com\> wrote:
> 
> > AHA - EUREKA!!!
> > 
> > Thanks for your very quick answers! After "studying" continuations for  
> > a couple of days, reading everthing I could find about, I think I  
> > finally understand it. Your answers made it all clear.
> > 
> > Michele

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [22 September 2005 03:07 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/9 "2005-09-22T03:07:33Z")

</div>

You've got good eyes. That's exactly what it is ... a call to the "callcc"  
method passing a block.

> **···**
>
> On Wednesday 21 September 2005 10:51 pm, rpardee@gmail.com wrote:
> 
> > What I don't understand is why the line:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;04 callcc { |cont| return cont}
> > 
> > Doesn't give an 'undefined local method or variable "callcc"' error.  
> > To my noob eyes that looks like a method call w/an accompanying block.
> 
> --  
> -- Jim Weirich jim@weirichhouse.org [http://onestepback.org](http://onestepback.org)  
> -----------------------------------------------------------------  
> "Beware of bugs in the above code; I have only proved it correct,  
> not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

---

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://rubytalk.org/u/michele)\
**Post date:** [22 September 2005 08:01 UTC](https://rubytalk.org/t/continuation-where-does-it-continue/20971/10 "2005-09-22T08:01:39Z")

</div>

I justed wanted to understand how continuations work and where it  
happens, so I made the simplest example possible I could think of. I  
wanted to start work one place, leave it in the middle, do some other  
work, and then continue from where I left.

Your answer as well as Pit Capitain's above solved the problem.
