# Question about callcc

**URL:** <https://www.ruby-forum.com/t/question-about-callcc/71820>\
**Category:** Ruby\
**Created:** [October 11, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/question-about-callcc/71820 "2006-10-11T18:32:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 11, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/question-about-callcc/71820/1 "2006-10-11T18:32:56Z")

</div>

Hi,

I’m trying to understand continuation.  
There’s some confusion in the following:

In top-level,

[1]  
callcc { |c| $label = c }  
#…some other code here  
$label.call

[2]  
$label = callcc { |c| c }  
#…some other code here  
$label.call

Are they same or different?  
As I understand it, callcc returns the last expression or return value  
of c.call.  
Thus, basically [1] and [2] should be same, right?  
But I’m not sure.  
Can anyone explain it, please?

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 11, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/question-about-callcc/71820/2 "2006-10-11T19:01:12Z")

</div>

Sam K. wrote:

> $label.call  
> Thus, basically [1] and [2] should be same, right?  
> But I’m not sure.  
> Can anyone explain it, please?

callcc returns the return value of its block, the first time. Later, it  
returns the value it was called with.  
If you test [2], you’ll see the error message:  
undefined method `call’ for nil:NilClass (NoMethodError)

Try  
$label = callcc {|c| c}  
$label.call(3)

and  
$label = callcc {|c| puts “first call”; c}  
$label.call(lambda{puts “second call”})

to understand what’s going on.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 11, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/question-about-callcc/71820/3 "2006-10-11T21:41:50Z")

</div>

On Thu, 12 Oct 2006 01:59:26 +0900, Carlos wrote:

> > callcc { |c| $label = c }  
> > As I understand it, callcc returns the last expression or return value  
> > Try  
> > $label = callcc {|c| c}  
> > $label.call(3)
> 
> and  
> $label = callcc {|c| puts “first call”; c}  
> $label.call(lambda{puts “second call”})
> 
> to understand what’s going on.

This example (run in straight ruby, not irb) should clarify what’s going  
on.

begin  
$label=nil  
$label=callcc{|c| c}  
puts “After the first callcc”  
$label.call  
rescue StandardError=\>e  
puts e  
end

begin  
$label=nil  
callcc{|c| $label=c}  
puts “After the second callcc”  
$label.call  
rescue StandardError=\>e  
puts e  
end

[bloom@cat-in-the-hat ~]$ ruby callcc.rb | head  
After the first callcc  
After the first callcc  
undefined method `call’ for nil:NilClass  
After the second callcc  
After the second callcc  
After the second callcc  
After the second callcc  
After the second callcc  
After the second callcc  
After the second callcc  
(infintie loop)

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 12, 2006, 2:07am UTC](https://www.ruby-forum.com/t/question-about-callcc/71820/4 "2006-10-12T02:07:24Z")

</div>

Hi Ken,

Ken B. wrote:

> On Thu, 12 Oct 2006 01:59:26 +0900, Carlos wrote:
> 
> > > callcc { |c| $label = c }  
> > > As I understand it, callcc returns the last expression or return value  
> > > Try  
> > > $label = callcc {|c| c}  
> > > $label.call(3)
> > 
> > and  
> > $label = callcc {|c| puts “first call”; c}  
> > $label.call(lambda{puts “second call”})
> > 
> > to understand what’s going on.
> 
> This example (run in straight ruby, not irb) should clarify what’s going  
> on.
> 
> begin  
> $label=nil  
> $label=callcc{|c| c}  
> puts “After the first callcc”  
> $label.call  
> rescue StandardError=\>e  
> puts e  
> end
> 
> begin  
> $label=nil  
> callcc{|c| $label=c}  
> puts “After the second callcc”  
> $label.call  
> rescue StandardError=\>e  
> puts e  
> end
> 
> [bloom@cat-in-the-hat ~]$ ruby callcc.rb | head  
> After the first callcc  
> After the first callcc  
> undefined method `call’ for nil:NilClass  
> After the second callcc  
> After the second callcc  
> After the second callcc  
> After the second callcc  
> After the second callcc  
> After the second callcc  
> After the second callcc  
> (infintie loop)

Although I still don’t understand what’s going on in the back,  
I see the difference.

Thank you very much.

Sam
