Support Forums

Full Version: [Tutorial] Timeout
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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.
First comment.
Thank's for the share man Oui
I don't use Ruby but thanks, I have learn but the little bit of code Smile
Nice share Thanks