04-06-2010, 07:01 PM
Ruby has a very nice timeout library, that can be uses in many different places including network application to applications that have time limits, for example a chess game. This is a short example on how to use the library.
Example code:
If you enter an answer before the 5 second timeout you will receive a message
saying "You answer was" plus you answer.
If you took longer than the time, it prints "** You took too long**"
This is a basic example on how to use the timeout library.
Example code:
Code:
require('timeout')
begin
Timeout::timeout(5) do
$stdout.puts("Give me an answer.")
a = gets
$stdout.puts("Your answer is #{a}")
end
rescue TimeoutError
$stdout.puts("\n**You took to long**")
end
If you enter an answer before the 5 second timeout you will receive a message
saying "You answer was" plus you answer.
If you took longer than the time, it prints "** You took too long**"
This is a basic example on how to use the timeout library.