One option is use Kernel.select, or, equivalently, IO.select.

Basically, select waits for input on some ios.  But it allows for a  
timeout value which I believe can be 0.

Another option is to use fcntl with F_SETFL and O_NONBLOCK.

e.g.:

requite 'fcntl'
STDIN.fcntl(Fcntl::F_SETFL,Fcntl::O_NONBLOCK)
STDIN.read

Will throw an Errno:EAGAIN if there is no input.

Note that gets waits for a newline so will still block.

c.


On Oct 3, 2007, at 12:46 PM, Stephen Ware wrote:

> Hi guys, sorry if this is a stupid question, but I've been reading
> around online and in 'Programming Ruby,' and I can't find anything.
>
> Is there a way to perform a non-blocking gets or getc?  As in a method
> that reads a character from stdin if there is one in the buffer or
> returns nil otherwise?  I'm writing a multi-threaded application that
> communicates over the internet using sockets, but I want the person  
> who
> runs the script on the host machine to be able to enter commands via
> stdin.  The 'gets' command hangs all my threads until it returns.
>
> Any suggestions?  Thanks!
> -Stephen
> -- 
> Posted via http://www.ruby-forum.com/.
>

--

[PGP Key available at www.keyserver.net - http://homepage.mac.com/ 
calfeld]