On Tue, 28 Mar 2006, brian yahn wrote:

> Wouldn't this:
>
>     puts Time.now
>     STDIN.wait unless STDIN.ready?
>     puts Time.now
>
>     puts STDIN.gets
>
> block until something is input?  Thats not what I want to do.  Unless
> wait is deceiving, I don't see how this would do what I want.


wait takes a timeout.

you may want to try io/nonblock:

   harp:~ > cat a.rb
   require 'io/nonblock'
   p STDIN.nonblock{ STDIN.gets }


   harp:~ > ruby a.rb </dev/null
   nil


   harp:~ > ruby a.rb <a.rb
   "require 'io/nonblock'\n"


but you have to realize there really are no ways to tell 'if something was
input' - only ways to tell if a read would block.  consider

   harp:~ > cat b.rb
   print 42
   sleep

   harp:~ > ruby b.rb|ruby a.rb
   ...


but we're blocking because we used 'gets' which reads till newline.  the only
other alternatives are giving a number of bytes to read or using something like
readpartial

   harp:~ > cat a.rb
   require 'io/nonblock'
   p STDIN.nonblock{ STDIN.readpartial(2) }

   harp:~ > cat b.rb
   STDOUT.sync = true
   print 42
   sleep

   harp:~ > ruby b.rb|ruby a.rb
   "42"
   ...

but even here you hang.


what are you trying to do exactly?  the std unix practive of giving '-' as a
command line arg when stdin is meant to be read can neatly avoid this problem.

regards.


-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama