From: Rick DeNatale Date: 2007-06-07T05:39:09+09:00 Subject: Re: code problems On 6/6/07, Robert Dober wrote: > > > > > Hi Rick I am still struggeling with continuations :(, as a matter of > fact I hoped that they would behave as described above, but they do > not, it seems. > > When I execute the code from your blog > def continuation > i = 0 > lola = nil > callcc {|cc| puts "In callcc";lola = cc} > puts "i is now #{i}" > i += 1 > lola > end > cont = continuation > # point of looping > cont.call > > I just get an endless loop, do you have an explanation for it? > Robert, you are right, it loops in ruby, but not in irb. I was trying to cook up a simple example and got hoist by irb's petard. What happens in ruby is that calling the continuation goes back to the method, which returns to the point before the cont.call and away we go again. Continuations normally aren't reused in the way I did in the example. I'm actually not sure why it works the way it does in irb. Here's a simpler example which shows the flow of control concept. rick@frodo:/public/rubyscripts$ cat testcontin.rb def continuation puts "Before callcc" callcc {|cc| puts "In callcc";return cc} puts "After callcc" end puts "Begin" cont = continuation puts "Got cont #{cont.inspect}" if cont cont.call puts "After call" end puts "All done" rick@frodo:/public/rubyscripts$ ruby testcontin.rb Begin Before callcc In callcc Got cont # After callcc Got cont nil All done -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/