From: Kristof Bastiaensen Date: 2004-04-12T09:34:26+09:00 Subject: Re: Continuation usage Hi, On Sun, 11 Apr 2004 12:41:50 +0200, Patrick Roemer wrote: > 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. > This looks very solid to me. Maybe you could make every Service a subclass of ContSession, and every session an instance of that session. It would look something like this: class ContSession def start(*args) callcc do |cc| @main = cc session(*args) end end def service(arg) callcc do |cc| @main = cc @cont.call(arg) end end def io(out) callcc do |cc| @cont = cc @main.call(out) end end private :io end class Guess < ContSession def session target = rand(100) inp = io("Please enter a number between 1 and 100.").to_i round = 1 while(inp != target) inp = io(inp < target ? "Higher" : "Lower").to_i round += 1 end "You found it in #{round} rounds." end private :session end irb(main):002:0> g = Guess.new # irb(main):003:0> g.start "Please enter a number between 1 and 100." irb(main):004:0> g.service(50) "Lower" irb(main):005:0> g.service(25) "Higher" irb(main):006:0> g.service(37) "Lower" irb(main):007:0> g.service(31) "Higher" irb(main):008:0> g.service(34) "You found it in 5 rounds."