a = 0 while( a ) puts a a += 1 # the code should quit if an interrupt of a character is given end
通过每次迭代,都不应该询问用户输入.
# check for exit condition Thread.new do loop do exit if gets.chomp == 'q' end end a = 0 loop do a += 1 puts a sleep 1 end
顺便说一下,你必须输入q< Enter>退出,因为这是标准输入的工作方式.