1

I would like to know how I would be able to run .gets but continue to run the program after a certain delay.

For example:

variable=gets.chomp
#somehow continue running program to do for example:
#after a delay (sleep 10)
puts "Hello? Are you still here? The program will timeout in 10 seconds."
#asks for input again
puts "Please enter ..."
variable=gets.chomp

Thank you for your time.

Pabi
  • 946
  • 3
  • 16
  • 47

1 Answers1

1
require 'timeout'
print "Input something within 10 seconds: "
begin
  foo = Timeout::timeout(10) {
    gets.chomp
  }
  puts "You said #{foo}"
rescue Timeout::Error
  puts
  puts "You are too late."
end
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thank you for your quick response. But may I ask what the two colons between Timeout and timeout and Error do? – Pabi Jul 13 '15 at 02:33
  • You could also say `Timeout.timeout` but not `Timeout.Error` (class methods get a pass at being called "incorrectly", and this is actually more common, but constants have to use the scope resolution operator and not the dot.) – Amadan Jul 13 '15 at 02:40