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.
I don't know how to implement the code for question 1. I wonder if
anyone there has a good idea.
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
El Domingo, 5 de Octubre de 2008, Li Chen escribió:
Hi all,
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.
I don't know how to implement the code for question 1. I wonder if
anyone there has a good idea.
-------------
ARRAY = %w{A B C D E}
ARRAY.each do |letter|
@t_input = Thread.new do
input = gets
puts "#{letter} - String entered by user: #{input}"
end
@t_timer= Thread.new do
sleep 3
puts "Timeout, going to next element..." @t_input.terminate
end
I ran the script(script a) in irb mode and I find it never jumps to next
element without input from outside/console, which is not what I want.
Here is my requirement for the timeout: 1) iterate an array 2)when
jumping from one element to the next wait for 5 seconds for the input.
If no input after 5 seconds then jump to the next element. If there is
an input then process the input. After that also jump to the next
element.
Also I ran two very short scripts (script b and script c) after reading
doc for Timeout. I find that one works and other doesn't work: without
input from the console the timeout does not work.
Any idea?
Thanks,
Li
####script a: without input the array never move forward ####
C:\Users\Alex>irb
irb(main):001:0> require 'timeout'
=> false
irb(main):002:0> a=(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):003:0>
irb(main):004:0* STDOUT.sync=true
=> true
irb(main):005:0>
irb(main):006:0* a.each do |e|
irb(main):007:1* print "> "
irb(main):008:1>
irb(main):009:1* begin
irb(main):010:2* Timeout.timeout(5) do
irb(main):011:3* x=gets
irb(main):012:3> puts "you entered: #{x}"
irb(main):013:3> end
irb(main):014:2> rescue Timeout::Error
irb(main):015:2> end
irb(main):016:1>
irb(main):017:1* puts e
irb(main):018:1> end
El Domingo, 5 de Octubre de 2008, Li Chen escribió:
Hi all,
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.
I don't know how to implement the code for question 1. I wonder if
anyone there has a good idea.
This works for me:
-------------
ARRAY = %w{A B C D E}
ARRAY.each do |letter|
@t_input = Thread.new do
input = gets
puts "#{letter} - String entered by user: #{input}"
end
@t_timer= Thread.new do
sleep 3
puts "Timeout, going to next element..." @t_input.terminate
end
@t_input.join @t_timer.terminate
end
It doesn't work for me. The timeout hangs out for ever if no input is
available.