From: Patrick Roemer Date: 2004-04-11T19:44:13+09:00 Subject: Continuation usage Once again I've been trying to get a grip on continuations, however, I'm quite unsure whether I've really got it this time. Could anybody please take a look at the example 'server' below. It seems to work when called from irb, but I'm unclear on how to test such a beast programmatically, so I suspect I somehow must have got it wrong again. TIA, Patrick class ContServer def initialize() @blocks={} # named services @mains={} # top-level continuations by session id @conts={} # service continuations by session id end def add(name,&block) @blocks[name]=block end def service(id,name='',inp='') callcc do |cc| @mains[id]=cc # remember top-level continuation if(@conts.key?(id)) # already in session? @conts[id].call(inp) # return to service continuation else ret=@blocks[name].call(id,self) # enter new session @conts.delete(id) # finish session ret end end end def io(id,out) callcc do |cc| @conts[id]=cc # remember service continuation @mains[id].call(out) # return to top-level continuation end end def ContServer::init cs=ContServer.new cs.add('guess') do |id,server| target=rand(100) inp=server.io(id,"Please enter a number between 1 and 100.").to_i round=1 while(inp!=target) inp=server.io(id,(inp < target ? "Higher" : "Lower")).to_i round += 1 end "You found it in #{round} rounds." end cs end end