From: Jim Weirich Date: 2005-09-21T23:36:30+09:00 Subject: Re: Continuation - where does it continue 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. Now we return a nil value) 10: doing other stuff 11: (since cc returned a nil the second time, we don't call the continuation this time) 12: end of work -- -- Jim Weirich jim@weirichhouse.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)