# Continuation Confusion

**URL:** <https://rubytalk.org/t/continuation-confusion/18025>\
**Category:** ruby-talk\
**Created:** [30 April 2005 18:58 UTC](https://rubytalk.org/t/continuation-confusion/18025 "2005-04-30T18:58:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blaine\_Buxton](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@Blaine\_Buxton](https://rubytalk.org/u/Blaine_Buxton)\
**Post date:** [30 April 2005 18:58 UTC](https://rubytalk.org/t/continuation-confusion/18025/1 "2005-04-30T18:58:09Z")

</div>

Hello,  
I was playing around with continuations by studying the generator code in Hal Fulton's Ruby Way book. I decided to re-implement the code to test my understanding. I decided to add the ability to backup the generator and started to get "weird" results. I started to pass the intermediate values on the stack and everything works. But, I would like to understand better why. Can anyone help me? Thanks in advance! Here's my code with comments and tests:

#playing with continuations  
#inspired by Generator code in Hal Fulton's book  
#I read it and then tried to recreate it to test my understanding  
#I add the ability to "backup" the generator. I've added tests as well.

> **···**
>
> #  
> #I have to pass the locals on the stack to get it to work correctly? Why is this?  
> #Is there something I am missing? I did use one less continuation, but it's  
> #just the continuation to continue the loop of the generator.
> 
> require 'test/unit'
> 
> class Generator  
> &nbsp;&nbsp;attr\_accessor :currentContext, :contextHistory  
> &nbsp;&nbsp;def initialize()  
> &nbsp;&nbsp;self.currentContext=nil  
> &nbsp;&nbsp;self.reset  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def next  
> &nbsp;&nbsp;&nbsp;&nbsp;callcc do |here|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.currentContext=here  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.contextHistory.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generatorLoop  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.contextHistory.last.call  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def backup  
> &nbsp;&nbsp;&nbsp;&nbsp;self.contextHistory.pop  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def answer(value, \*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;callcc do |cont|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.contextHistory.push(cont)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.currentContext.call(value)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;args  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def reset()  
> &nbsp;&nbsp;&nbsp;&nbsp;self.contextHistory=[]  
> &nbsp;&nbsp;end  
> end
> 
> class Fibonacci \< Generator  
> &nbsp;&nbsp;def generatorLoop  
> &nbsp;&nbsp;&nbsp;&nbsp;previous, current=0,1  
> &nbsp;&nbsp;&nbsp;&nbsp;loop do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#I pass the previous and current local variables as arguments just so they are returned  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#If I don't pass them, weird things happen which makes me think Ruby  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#continuations only save part of the state or treats closures special on the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#stack. The variables are local to the method and not the closure. But, I still  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#think the method's local state should be saved on the stack as well. Am I missing  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#something?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#works  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;previous, current = answer(current, previous, current)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#doesn't work, but I think it should, what am I missing?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#answer(current)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;previous, current = current, previous + current  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> class TestFibonacci \< Test::Unit::TestCase  
> &nbsp;&nbsp;attr\_accessor :to\_test  
> &nbsp;&nbsp;def setup  
> &nbsp;&nbsp;&nbsp;&nbsp;self.to\_test=Fibonacci.new  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def teardown  
> &nbsp;&nbsp;&nbsp;&nbsp;self.to\_test=nil  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def test\_next  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(1, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(2, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(3, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(5, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(8, self.to\_test.next)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def test\_back  
> &nbsp;&nbsp;&nbsp;&nbsp;(1..6).each { self.to\_test.next }  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(13, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;self.to\_test.backup  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(13, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;self.to\_test.backup  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(8, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(13, self.to\_test.next)  
> &nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(21, self.to\_test.next)  
> &nbsp;&nbsp;end
> 
> end  
> --  
> Blaine Buxton, Mad Scientist In Training  
> My Amps: Smalltalk, Lisp, and Ruby  
> [http://www.blainebuxton.com](http://www.blainebuxton.com)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Is your PC infected? Get a FREE online computer virus scan from McAfee® Security. [http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963](http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963)

---

<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:** [1 May 2005 15:30 UTC](https://rubytalk.org/t/continuation-confusion/18025/2 "2005-05-01T15:30:11Z")

</div>

Blaine Buxton schrieb:

> But, I still think the method's local state should be saved on the  
> stack as well. Am I missing something?

Hi Blaine,

if you look at this small example, you see that local variables are indeed \*not\* saved by continuations:

&nbsp;&nbsp;&nbsp;v = 1  
&nbsp;&nbsp;&nbsp;c = callcc { |cc| cc }  
&nbsp;&nbsp;&nbsp;puts( v, c )  
&nbsp;&nbsp;&nbsp;if Continuation === c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.call( 5 )  
&nbsp;&nbsp;&nbsp;end

Output:

&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;&nbsp;#\<Continuation:0x2a6b738\>  
&nbsp;&nbsp;&nbsp;2  
&nbsp;&nbsp;&nbsp;5

Feel free to ask if you need more help.

Regards,  
Pit
