# Continuation in ruby

**URL:** <https://www.ruby-forum.com/t/continuation-in-ruby/53757>\
**Category:** Ruby\
**Created:** [February 14, 2006, 8:26am UTC](https://www.ruby-forum.com/t/continuation-in-ruby/53757 "2006-02-14T08:26:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 14, 2006, 8:26am UTC](https://www.ruby-forum.com/t/continuation-in-ruby/53757/1 "2006-02-14T08:26:27Z")

</div>

Hi all.

I’ve written the following code to learn continuations in ruby only to  
fail. Please have a look at:

def print\_num  
for i in (1…10)  
puts i  
if i % 2 == 0; callcc{|c| return c}; end  
end  
end

cont = print\_num  
puts “Yae, I love even~”  
cont.call  
puts “Bye”

This code produces:

C:\WINDOWS\system32\cmd.exe /c ruby a.rb  
1  
2  
Yae, I love even~  
3  
4  
Yae, I love even~  
5  
6  
Yae, I love even~  
7  
8  
Yae, I love even~  
9  
10  
Yae, I love even~  
Yae, I love even~  
a.rb:10: undefined method `call’ for 1…10:Range (NoMethodError)  
shell returned 1  
Hit any key to close this window…

This is contrary to my intuition. I’ve called cont.call only once, and  
cont.call should put the following command, put “bye”, into stack. I  
can’t figure out the reason why cont.call is called indefinitely.

Best,  
Minkoo S.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 14, 2006, 9:25am UTC](https://www.ruby-forum.com/t/continuation-in-ruby/53757/2 "2006-02-14T09:25:35Z")

</div>

On 2/14/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all.
> 
> I’ve written the following code to learn continuations in ruby only to  
> fail. Please have a look at:

From another learner  
Look at my following try  
def print\_num  
for i in (1…10)  
puts i  
if i % 2 == 0; callcc{|c| return c}; end  
puts “\*\* cont.call brings you here”  
end  
return nil  
end

cont = print\_num  
puts “Yae, I love even~”  
cont.call if cont  
puts “Bye”

Hope this helps  
Prasad

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 14, 2006, 9:28am UTC](https://www.ruby-forum.com/t/continuation-in-ruby/53757/3 "2006-02-14T09:28:35Z")

</div>

Minkoo,

When you call cont.call, the control goes right back to the end of  
callcc block with all the context in tact. So your program’s control  
flow looks like this:

1. cont = print\_num # calls print\_num method
2. callcc {|c| return c} # at i % 2 == 0, print\_num returns with the  
continuation as a return value, which is now assigned to cont
3. puts “Yae…”
4. cont.call # now the control goes back to “}” of callcc with the  
variable i == 2
5. for loop continues
6. go to step 2 unless i == 10
7. at i == 10, callcc block returns one final time
8. puts …
9. cont.call
10. now that i == 10 for loop finishes and the return value of  
print\_num at this time is (1…10), because “for loop” is essentially  
(1…10).each {|i|} in disguise, and #each returns self
11. puts …
12. cont.call # argh! this time you’re calling (1…10).call
13. NoMethodError exception

This is what has happened.

Hope this helps.

Jesse

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 14, 2006, 10:54am UTC](https://www.ruby-forum.com/t/continuation-in-ruby/53757/4 "2006-02-14T10:54:07Z")

</div>

Now I get it.

So the continuation returned from print\_num by cont = print\_num  
contains not only the context within print\_num method, but also the  
context of “cont = print\_num”. In other words, cont contains (1)  
print\_num was called, (2) location within print\_num, (3) and variables  
in its stack.

Thank you, folks.

Best,  
Minkoo S.
