Ruby循环直到出问题

时间:2012-03-10 23:23:07

标签: ruby

对原始问题进行澄清。我需要一个将在ruby中运行的循环,并允许我进入命令停止进入控制台(可能使用gets)然后循环将停止。

干杯 马丁

1 个答案:

答案 0 :(得分:7)

我认为你可以使用这样的东西:

# 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