Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sorry that I don't have an answer to your larger problem of sending a signal after X seconds to a process, but it seems that all you want to do is timeout after X seconds of waiting for input, and if that's the case then I'd say you are looking for Kernel.select :D</p> <p>I've personally never used this, but after doing a google for "non-blocking gets", and subsequently exploring links, I found these two to be invaluable discussions:</p> <p><a href="http://www.ruby-forum.com/topic/126795" rel="nofollow noreferrer">http://www.ruby-forum.com/topic/126795</a> (Discussion of multi-threaded gets)</p> <p><a href="http://www.ruby-forum.com/topic/121404" rel="nofollow noreferrer">http://www.ruby-forum.com/topic/121404</a> (Explanation of Kernel.select in 2nd post)</p> <p>Here's a sample of how to use it. This will print out your prompt and wait for input... If there is no input after five seconds, then the program will end. If there is input, as soon as there is input it will spit it back out and end... Obviously you can modify this for your own purposes.</p> <pre><code>def prompt STDOUT.write "&gt; " STDOUT.flush end def amusing_messages [ "You must enter something!", "Why did you even start me if you just wanted to stare at me?", "Isn't there anything better you could be doing?", "Just terminate me already... this is getting old", "I'm waiting..."] end prompt loop do read_array, write_array, error_array = Kernel.select [STDIN], nil, nil, 5 if read_array.nil? puts amusing_messages[rand(amusing_messages.length)] else puts "Result is: #{read_array[0].read_nonblock(30)}" end prompt end </code></pre> <p>It's probably not as elegant as you might like, but it definitely gets the job done without mucking around with threads. Unfortunately, this won't help you should you want something more robust (timer/sending a signal to the process), and sadly, I have no clue if this works in JRuby. Would love to know if it does though :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload