From: Tim Pease Date: 2007-10-31T00:28:19+09:00 Subject: Re: loop until a key (i.e. enter or ESC) is pressed 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