如何运行.gets但在Ruby中出现一段延迟后继续运行程序

时间:2015-07-13 02:22:17

标签: ruby delay gets

我想知道如何运行.gets但是在一段延迟后继续运行程序。

例如:

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

感谢您的时间。

1 个答案:

答案 0 :(得分: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