回答
我想你可以使用这样的事情:
# Initialize the input queue. This is where the user-created info will be stored
$QUEUE = []
def pending
old = $QUEUE
$QUEUE = []
old
end
t = Thread.new do
loop do
# Ask the user for something
print "Enter info here: "
# Read information in
$QUEUE << gets.chomp
end
end
# Example code utilizing this; you can do whatever you like with the queue
2.times do
sleep 5
# Print the list out
puts "\nYou entered: \n" << pending.join("\n")
end
您也可以使用'exit'而不是'Thread.main.exit'。 – 2012-03-10 23:40:06
这只会退出已启动的线程。 – Jwosty 2012-03-10 23:41:21
我想你会把['Kernel#exit'](http://apidock.com/ruby/Kernel/exit)与['Thread.exit']混淆(http://apidock.com/ruby/Thread/exit/类)在这里。 'exit'只会终止完整的脚本,这似乎是合适的。第三个例子中的额外逻辑可以通过不设置变量并仅检查主循环内部的't.stop?'来实现。 – 2012-03-10 23:55:00
- 1. 循环出现问题
- 2. 循环直到出现错误424
- 3. 我的高级循环出现问题
- 4. 我的while循环出现问题
- 5. 使用while循环时出现循环元素的问题
- 6. 批循环/输出问题
- 7. 并行For循环 - 添加到列表时出现问题
- 8. Ruby中嵌套循环的问题
- 9. 循环直到(py)
- 10. 转到问题和for循环问题
- 11. XSL到XML循环问题
- 12. Do/While循环输出时出现问题
- 13. 程序结束时出现循环问题,无法退出
- 14. 显示输出,直到循环
- 15. 循环程序,直到0(退出)(java)
- 16. 循环如果并且直到结果发现然后退出
- 17. 将数据附加到现有文本文件时出现循环问题
- 18. Ruby循环输出重复
- 19. 循环问题
- 20. 使用while循环vs foreach/for循环会出现性能问题吗?
- 21. 循环访问数据 - Ruby
- 22. 安装sqlite3-ruby时出现问题!
- 23. 使用嵌套for循环出现问题
- 24. Java:在替换循环中的字符串时出现问题
- 25. 在循环中打印时出现问题:对于
- 26. docker在执行python循环中的time.sleep(1)时出现问题
- 27. 使用Django标签的循环出现问题
- 28. 电子邮件中的foreach循环出现问题
- 29. 使用数组和while循环时出现问题
- 30. Javascript - 通过数组索引循环出现的问题
你应该添加一些标签(例如
until
或loop
),这样,如果别人有同样的问题,他们可以找到这个问题。 – Jwosty 2012-03-10 23:56:21我认为这个问题没有包含更多标签,没有什么比这里涉及的纯Ruby更好。应该避免像'loop'这样的标签。 – 2012-03-10 23:59:35
嗯,好点。这些只是我头脑中的第一件事,但我不想提供真正的标签 – Jwosty 2012-03-11 00:05:06