且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不暂停脚本的情况下收听 STDIN 输入?

更新时间:2022-04-07 22:09:16

不确定示例中要继续运行"的命令在哪里.试试这个小脚本:

Not sure where are the commands you want to "continue running" in your example. Try this small script:

Thread.new do
  loop do
    s = gets.chomp
    puts "You entered #{s}"
    exit if s == 'end'
  end
end

i = 0
loop do
  puts "And the script is still running (#{i})..."
  i += 1
  sleep 1
end

从 STDIN 读取是在一个单独的线程中完成的,而主脚本继续工作.

Reading from STDIN is done in a separate thread, while the main script continues to work.