Gets() -- Need to skip when no response after 20seconds

Code:

puts “Print your age”
a=gets()
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”

Need:

a=gets() is waiting for input. I need to wait only 20 seconds. If therre
is no input for 20 seconds then script should continue the next line

Thanks
Raveendran P

Raveendran .P wrote:

Code:

puts “Print your age”
a=gets()
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”

Need:

a=gets() is waiting for input. I need to wait only 20 seconds. If therre
is no input for 20 seconds then script should continue the next line

Thanks
Raveendran P
http://raveendran.wordpress.com

require ‘timeout’
a = nil
begin
puts “Print your age”
Timeout.timeout(20) do
a = gets
end
rescue Timeout::Error
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”
end

Warning: like most programs, this may not work as desired under

Windows

Hi Brain and Senault,

    The script is still waiting for input from User.

** But the same timeout code works for while loop printing up to 1000.

I am using Windows XP Service pack 2, Ruby 1.8.7

Thanks

Brian C. wrote:

Raveendran .P wrote:

Code:

puts “Print your age”
a=gets()
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”

Need:

a=gets() is waiting for input. I need to wait only 20 seconds. If therre
is no input for 20 seconds then script should continue the next line

Thanks
Raveendran P
http://raveendran.wordpress.com

require ‘timeout’
a = nil
begin
puts “Print your age”
Timeout.timeout(20) do
a = gets
end
rescue Timeout::Error
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”
end

Warning: like most programs, this may not work as desired under

Windows

Hi,

2010/7/14 Raveendran .P [email protected]:

Need:
a = nil
Windows

If you try the code with Ruby 1.9.1
(http://rubyforge.org/frs/download.php/71495/rubyinstaller-1.9.1-p429.exe),
It will work.

Regards,
Park H.

Hi Park,

If you try the code with Ruby 1.9.1
(http://rubyforge.org/frs/download.php/71495/rubyinstaller-1.9.1-p429.exe),
It will work.

But my environment is Ruby 1.8.7. I need to implement this particular
task in existing application.

Hi,

2010/7/14 Raveendran .P [email protected]:

Hi Park,

If you try the code with Ruby 1.9.1
(http://rubyforge.org/frs/download.php/71495/rubyinstaller-1.9.1-p429.exe),
It will work.

But my environment is Ruby 1.8.7. I need to implement this particular
task in existing application.

Try this code:

require ‘Win32API’

@@kbhit = Win32API.new(“msvcrt”, “_kbhit”, [], ‘I’)

for i in 0…19
unless @@kbhit.call.zero?
a = gets()
break
end
sleep 1
end
if i==19
puts “Timeout. 20 Seconds Gone. Please re-run the Applciation”
end

Regards,
Park H.

On Wed, Jul 14, 2010 at 5:48 PM, Raveendran .P [email protected]
wrote:

Hi Park,

If you try the code with Ruby 1.9.1
(http://rubyforge.org/frs/download.php/71495/rubyinstaller-1.9.1-p429.exe),
It will work.

But my environment is Ruby 1.8.7. I need to implement this particular
task in existing application.

if select([$stdin], nil, nil, 20)
p gets
end

On Wed, Jul 14, 2010 at 12:07 PM, Michael F.
[email protected]wrote:

if select([$stdin], nil, nil, 20)
p gets
end

IIRC, the last time I tried that on Windows (a while back) the select
returned on the first char and then gets acted as usual, blocking until
EOL.
I would like to know if that actually works.

Ammar

Hi,

Thanks Park. Your code helps me to complete the task.

If any other solution please update here…

The below code also not working in Windows XP and Ruby 1.8.7

if select([$stdin], nil, nil, 20)
p gets
end

Le 14 juillet à 08:22, Raveendran .P a écrit :

Need:

a=gets() is waiting for input. I need to wait only 20 seconds.

Look at the Timeout standard library :

begin
Timeout.timeout(20) do
a = gets()
end
rescue Timeout::Error
puts “Timeout. 20 seconds gone, etc”
end

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs