michelemendel / gmail.com schrieb:
> Why does the following code print the line "doing other stuff" twice?
> 
> def cc
>     puts "do A"
>     puts "----> 1. #{caller}"
>     callcc { |cont| return cont}

The continuation continues here, after the call to callcc.

>     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

   puts "calling cc"
   cont = cc()
   if cont
     puts "doing other stuff"
     cont.call()
   end
   puts "end of work"
   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