Tim Pease
10/30/2007 3:28:00 PM
On Oct 30, 2007, at 8:43 AM, Wurzel Cidermaker wrote:
> I'm very new to Ruby (3 days).
>
> How do I escape from the loop below? - Without doing a CTRL-C.
>
> # loop forever...but I want to exit from it when a key is pressed
>
> def loopy
> i = 0
> while true
> i+=1
> puts i
> end
> end
> loopy
You will need to run your loopy method in a separate thread, and then
kill that thread when the user presses any key on the keyboard ...
t = Thread.new do
i = 0
while true
i += 1
puts i
end
end
gets
t.kill
The above code won't work with any keypress -- you have to hit enter
in order to stop the program.
Blessings,
TwP