Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > loop until a key (i.e. enter or ESC) is pressed

Reply
Thread Tools

loop until a key (i.e. enter or ESC) is pressed

 
 
Wurzel Cidermaker
Guest
Posts: n/a
 
      10-30-2007
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Tim Pease
Guest
Posts: n/a
 
      10-30-2007
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

 
Reply With Quote
 
 
 
 
Wurzel Cidermaker
Guest
Posts: n/a
 
      10-30-2007
Tim Pease wrote:
>
> 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



That does work properly.

Is starts to display

1
2
3
...
...
127

then stops and I presume is waiting for the "gets"

To put this into some context;

I have a socket client program that continously reads data from a
server, the only way I have currently to stop it is to do a CTRL-C, I
want/would expect a more elegant way of performing an exit on a loop.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      10-30-2007

On Oct 30, 2007, at 9:50 AM, Wurzel Cidermaker wrote:

> then stops and I presume is waiting for the "gets"
>
> To put this into some context;
>
> I have a socket client program that continously reads data from a
> server, the only way I have currently to stop it is to do a CTRL-C, I
> want/would expect a more elegant way of performing an exit on a loop.


start this before you fire up your socket stuff

reaper =
Thread.new do
loop do
Kernel.exit if gets == 'exit'
end
end

if you type exit, your program will exit. check out highline or
search the archives if you want something more elegant - exiting a
loop and accepting cross platform user input is a real can of worms
made complex by the lack of getch() on *nix platforms, the
idiosyncracies of termninal attributes, and buffering issues with pty
vs not.

regards

a @ http://codeforpeople.com/
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




 
Reply With Quote
 
Wurzel Cidermaker
Guest
Posts: n/a
 
      10-30-2007
ara.t.howard wrote:
> On Oct 30, 2007, at 9:50 AM, Wurzel Cidermaker wrote:
>
>> then stops and I presume is waiting for the "gets"
>>
>> To put this into some context;
>>
>> I have a socket client program that continously reads data from a
>> server, the only way I have currently to stop it is to do a CTRL-C, I
>> want/would expect a more elegant way of performing an exit on a loop.

>
> start this before you fire up your socket stuff
>
> reaper =
> Thread.new do
> loop do
> Kernel.exit if gets == 'exit'
> end
> end
>
> if you type exit, your program will exit. check out highline or
> search the archives if you want something more elegant - exiting a
> loop and accepting cross platform user input is a real can of worms
> made complex by the lack of getch() on *nix platforms, the
> idiosyncracies of termninal attributes, and buffering issues with pty
> vs not.
>
> regards
>
> a @ http://codeforpeople.com/


thanks for your response but I struggling to implement it.

can you please fix what I have below?

# example

def loopy

loop do

puts Time.now
sleep 1

end

end

reaper =
Thread.new do
loop do
Kernel.exit if gets == 'exit'
end
end

loopy
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      10-30-2007

On Oct 30, 2007, at 10:38 AM, Wurzel Cidermaker wrote:

>
> def loopy
>
> loop do
>
> puts Time.now
> sleep 1
>
> end
>
> end
>
> reaper =
> Thread.new do
> loop do
> Kernel.exit if gets == 'exit'
> end
> end
>
> loopy


sorry, my fault, that 'gets' will have a newline on it, this works:

cfp:~ > ruby a.rb
Tue Oct 30 11:01:32 -0600 2007
Tue Oct 30 11:01:33 -0600 2007
Tue Oct 30 11:01:34 -0600 2007
Tue Oct 30 11:01:35 -0600 2007
Tue Oct 30 11:01:36 -0600 2007
exit



cfp:~ > cat a.rb
def loopy
loop do
puts Time.now
sleep 1
end
end

reaper =
Thread.new do
loop do
Kernel.exit if gets =~ /exit/
end
end

loopy


regards.

a @ http://codeforpeople.com/
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




 
Reply With Quote
 
Wurzel Cidermaker
Guest
Posts: n/a
 
      10-30-2007

thanks...but...

doesn't work on my Windows XP box!

it prints the time once, then waits for input.

any ideas why?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      10-30-2007

On Oct 30, 2007, at 11:17 AM, Wurzel Cidermaker wrote:

> thanks...but...
>
> doesn't work on my Windows XP box!
>
> it prints the time once, then waits for input.
>
> any ideas why?


you can't easily multi-plex io on windows with ruby. search the
archvies but, mostly, this is very slippery and you have to have a
deep understanding of ruby's thread model to accomplish it. search
for 'threads blocking windows' in the archives. on *nix it's quite
easy - with windows you are going to have to adapt to a multi process
model i think. it's hard to say without knowing the details of your
program.

sorry!

a @ http://codeforpeople.com/
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




 
Reply With Quote
 
Dan Yoder
Guest
Posts: n/a
 
      11-02-2007
Another possible approach is to use trap to catch the TERM or INT
signals. From there, you can use a variety of strategies to shut the
program down gracefully. You can either hit CTRL-C (or CTRL-D if you
catch TERM) or write a very simple second program that sends the
signal to the first program (start and stop scripts, basically). The
second program will need the process id for the first, which could be
written out to a designated file using $$.

I don't know if trap is supported on Vista. One method of implementing
trap on Windows I've seen (but never used personally) is to wrap the
Kernel's trap method, like this:

def trap(signal)
Kernel::trap(signal){yield}
Thread.new{loop{sleep 1}}
end

You can easily simulate a trap by simply checking a designated file
(you can use $$ to generate a unique filename) for a termination
string (like TERM, ASCII 4) on each iteration of the loop. Then your
shutdown script would simply write the termination string to the file
and exit. The listener script would see the exit string and shut down.

As noted by Ara, this problem is much simpler on Unix, where threading
is better supported. In the next version of Ruby, native threading
will be supported (including on Vista) so hopefully you won't need
these kinds of tricks.

Dan

---

Dan Yoder
http://dev.zeraweb.com/
Ruby And JavaScript Consulting



On Oct 30, 11:41 am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Oct 30, 2007, at 11:17 AM, Wurzel Cidermaker wrote:
>
> > thanks...but...

>
> > doesn't work on my Windows XP box!

>
> > it prints the time once, then waits for input.

>
> > any ideas why?

>
> you can't easily multi-plex io on windows with ruby. search the
> archvies but, mostly, this is very slippery and you have to have a
> deep understanding of ruby's thread model to accomplish it. search
> for 'threads blocking windows' in the archives. on *nix it's quite
> easy - with windows you are going to have to adapt to a multi process
> model i think. it's hard to say without knowing the details of your
> program.
>
> sorry!
>
> a @http://codeforpeople.com/
> --
> it is not enough to be compassionate. you must act.
> h.h. the 14th dalai lama



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows: How to sleep until key is pressed Dilbert Perl Misc 8 02-25-2010 12:18 AM
Enter Key H*ll, control enter key submit button BrianDH ASP .Net Web Controls 3 01-12-2005 08:29 PM
Control postback events when enter key is pressed jhoge123@yahoo.com ASP .Net 0 12-02-2004 02:21 PM
how to disable post back if enter key is pressed on th form gh0st54 ASP .Net 2 09-27-2004 06:41 PM
How to detect that a key is being pressed, not HAS been pressed earlier!?? Rune Python 6 01-29-2004 12:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57