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

···

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

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

···

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

Need:

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

--
What would be the point of cyphering messages that very clever enemies
couldn't break? You'd end up not knowing what they thought you thought
they were thinking... (Terry Pratchett, in The Fifth Elephant)

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

···

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

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 Candler 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

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

Hi,

···

2010/7/14 Raveendran .P <jazzezravi@gmail.com>:

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 Candler 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

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 Heesob

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.

···

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

Hi,

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 Heesob

···

2010/7/14 Raveendran .P <jazzezravi@gmail.com>:

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 5:48 PM, Raveendran .P <jazzezravi@gmail.com> wrote:

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

--
Michael Fellinger
CTO, The Rubyists, LLC

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

···

On Wed, Jul 14, 2010 at 12:07 PM, Michael Fellinger <m.fellinger@gmail.com>wrote:

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

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

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