2016-04-23 55 views

回答

3

您可以使用包含在标准库中的Timeout模块。如果你想要rescue它会在超时时产生一个Timeout::Error

require 'timeout' 
x = 10 
begin 
    status = Timeout::timeout(x) { 
    printf "Input: " 
    gets 
    } 
    puts "Got: #{status}" 
rescue Timeout::Error 
    puts "Input timed out after #{x} seconds" 
end 
0
require "timeout" 

Timeout.timeout(x) do 
    s = gets 
    ... 
end 
相关问题