user4493605 user4493605 - 2 years ago 87
Ruby Question

Timeout if no user input for `x` seconds

How would I program a chunk of code in Ruby, which would timeout or exit if no user input is entered for

amount of time?

I don't have a half completed script too better convey my question, or even a pseudo-code concept algorithm.

Answer Source

You can use the Timeout module that is included in the standard libraries. It will raise a Timeout::Error on timeout if you want to rescue it.

require 'timeout'
x = 10
  status = Timeout::timeout(x) {
    printf "Input: "
  puts "Got: #{status}"
rescue Timeout::Error
  puts "Input timed out after #{x} seconds"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download