Joel VanderWerf
8/28/2008 7:45:00 PM
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