gpt4 book ai didi

ruby - 如何超时gets.chomp

转载 作者:数据小太阳 更新时间:2023-10-29 07:17:41 26 4
gpt4 key购买 nike

我正在尝试编写一个程序,要求用户在三秒内使用 gets.chomp 回答问题,否则答案将自动返回 false。

我想出了除了超时部分之外的所有内容,我想知道是否有人可以提供帮助。

最佳答案

你可以使用timeout标准库

require "timeout"

puts "How are you?"
begin
Timeout::timeout 5 do
ans = gets.chomp
end
rescue Timeout::Error
ans = nil
end
puts (ans || "User did not respond")

阅读更多关于图书馆的信息 http://www.ruby-doc.org/stdlib-2.1.5/libdoc/timeout/rdoc/Timeout.html

关于ruby - 如何超时gets.chomp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27412804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com