user input timeout.

R

Rico Knoop

i'v bin programming for about 2 week now and just about completed
http://pine.fm/LearnToProgram/

but for a change of pace i started something of my own but i cant seem
to get my program to wait X-seconds for user input.

basicly what i got is:

input= "empty"
input= gets.chomp

if input == "empty"
puts "no input"
else
puts input
end

all i want it to do is wait like 10 seconds and if there has bin no user
input it should just continue with input= "empty"

i feel like i'm missing something real simple

thnx in advance

-Bakakyoo-
 
S

Shu Cao

results =3D select [STDIN], nil, nil, X-seconds
if !results
puts "no input"
else
puts gets.chomp
end
 
J

Jesús Gabriel y Galán

i'v bin programming for about 2 week now and just about completed
http://pine.fm/LearnToProgram/

but for a change of pace i started something of my own but i cant seem
to get my program to wait X-seconds for user input.

basicly what i got is:

input=3D "empty"
input=3D gets.chomp

if input =3D=3D "empty"
=A0puts "no input"
else
=A0puts input
end

all i want it to do is wait like 10 seconds and if there has bin no user
input it should just continue with input=3D "empty"

i feel like i'm missing something real simple

Timeout is in the standard lib:

require 'timeout'

begin
answer =3D Timeout::timeout(5) do
gets
end
rescue Timeout::Error
answer =3D "empty"
end

puts answer

Jesus.
 
R

Rico Knoop

Jesús Gabriel y Galán said:
Timeout is in the standard lib:

require 'timeout'

begin
answer = Timeout::timeout(5) do
gets
end
rescue Timeout::Error
answer = "empty"
end

puts answer

Jesus.


it didn't seem to work,
it just kept waiting for me to input something.
i even tried to run just that code.
its still blinking, waiting for input on the background.

am i doing something wrong?
 
C

Colin Bartlett

it didn't seem to work,
it just kept waiting for me to input something.
i even tried to run just that code.
its still blinking, waiting for input on the background.

am i doing something wrong?

It might depend on which operating system and/or Ruby version you are using.
I tried the code underneath on Windows Vista, using the Ruby Windows versions,
and Ruby. It works for me using Ruby 1.9 for Windows.
But using Ruby 1.8.6 and using the JRuby 1.8 equivalent seemed to work,
or rather not work, in the way you described in your latest post.

Colin Bartlett
 
C

Colin Bartlett

[* same post as just made, but this time I've remembered to add the code *]

it didn't seem to work,
it just kept waiting for me to input something.
i even tried to run just that code.
its still blinking, waiting for input on the background.

am i doing something wrong?

It might depend on which operating system and/or Ruby version you are using.
I tried the code underneath on Windows Vista, using the Ruby Windows versions,
and Ruby. It works for me using Ruby 1.9 for Windows.
But using Ruby 1.8.6 and using the JRuby 1.8 equivalent seemed to work,
or rather not work, in the way you described in your latest post.

Colin Bartlett

require "timeout"
def gets_timeout( prompt, secs )
puts
print prompt + "[timeout=#{secs}secs]: "
Timeout::timeout( secs ) { gets }
rescue Timeout::Error
puts "*timeout"
nil # return nil if timeout
end

rr = gets_timeout "Do not input. Wait for timeout. ", 5
p rr #=> nil
rr = gets_timeout "Input something before timeout: ", 10
p rr #=> "something\n"
 
J

Jesús Gabriel y Galán

It might depend on which operating system and/or Ruby version you are usi= ng.
I tried the code underneath on Windows Vista, using the Ruby Windows vers= ions,
and Ruby. It works for me using Ruby 1.9 for Windows.
But using Ruby 1.8.6 =A0and using the JRuby 1.8 equivalent seemed to work= ,
or rather not work, in the way you described in your latest post.

I tested my version in Ubuntu and it worked fine.

Jesus.
 
C

Charles Oliver Nutter

It might depend on which operating system and/or Ruby version you are usi= ng.
I tried the code underneath on Windows Vista, using the Ruby Windows vers= ions,
and Ruby. It works for me using Ruby 1.9 for Windows.
But using Ruby 1.8.6 =C2=A0and using the JRuby 1.8 equivalent seemed to w= ork,
or rather not work, in the way you described in your latest post.

At least for JRuby, we can't do a proper select on stdio because the
JVM does not provide such a capability. So timeout can't interrupt
stdio.

- Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top