我想知道如何运行.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
感谢您的时间。
答案 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