botp
10/5/2008 2:07:00 PM
On Sun, Oct 5, 2008 at 8:36 PM, Li Chen <chen_li3@yahoo.com> wrote:
> I iterate an array. During jumping from one element to the next one I
> need to get user input from the console. 1) If not input after 5 seconds
> I have to jump to the next element. 2)If there is an input within 5
> seconds I go for processing the input and then jump to the next
> element.
how about Timeout ?
pardon this crude example,
botp@jedi-hopeful:~$ cat test.rb
require 'timeout'
a=(1..10).to_a
STDOUT.sync=true
a.each do |e|
print "> "
begin
Timeout.timeout(5) do
x=gets
puts "you entered: #{x}"
end
rescue Timeout::Error
end
puts e
end
botp@jedi-hopeful:~$ ruby -v test.rb
ruby 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]
> 1
> 2
> hello
you entered: hello
3
> 4
> 5
> world
you entered: world
6
> 7
> 8
> 9
> 10
botp@jedi-hopeful:~$