Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] Timeout
#1
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.
Reply


Messages In This Thread
[Tutorial] Timeout - by Wolskie - 04-06-2010, 07:01 PM
RE: [Tutorial] Timeout - by JesusOfSuburbia - 05-03-2010, 02:07 AM
RE: [Tutorial] Timeout - by Julie - 05-09-2010, 03:18 PM
RE: [Tutorial] Timeout - by Bursihido - 12-13-2010, 10:57 PM

Forum Jump:


Users browsing this thread: 6 Guest(s)