From: Joel VanderWerf Date: 2008-08-29T04:44:58+09:00 Subject: Re: Subtle bug with catch/throw. Am I missing something? Patrick Li wrote: > Hi, > I'm having trouble locating this bug that I'm having. I think it's > because I don't completely understand Ruby's throw/catch mechanism. > > I'm trying to write a simple example that demonstrates the same problem, > but I can't track down the bug. > > Here's my code: Is there any subtleties that may cause throw/catch to > not behave as expected? > > def idle(engine,key) > catch(:exit) do <-- Catch :exit > while engine.nextFrame(key) > engine.drawFrame do > drawInContext(@img, 0, 0, 1) > end > > @enterEvent.listen do > @state = method(:active) > puts "player entered" > throw :exit <-- Throw :exit > end > end > end > end > > Gives me: "in `throw': uncaught throw `exit' (NameError)" I'm guessing that the problem is that the listen block has been saved away and is no longer above the #catch on the stack. Here's a simpler example: def listen catch(:exit) do @listeners << proc {throw :exit} end end @listeners = [] listen @listeners[0].call This produces the same error message. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407