views:

219

answers:

1

The standard Ruby library "io/wait" provides a method on IO objects ready? that returns non-nil if there is input available, nil or false otherwise. I know some methods like sysread and syswrite are not safe to use with higher level methods such as gets and read, and wanted to know if ready? was safe to mix with the higher level methods. The ready method seems rather useful and perhaps more elegant than relying on IO.select, but, surprisingly enough, I haven't seen it used much. There is nothing in the documentation to indicate it would be unsafe to use with read or gets, I just want to be sure I'm not mixing incompatible methods that could ultimately result in unexpected behavior when reading/writing sockets.

Edit: I suppose I should mention I am using Ruby 1.8.7.

+1  A: 

I don't have a lot of experience with Ruby, but I have a heck of a lot of experience with libc, and my opinion is that yes, it is safe. Odds are pretty good that "ready" is implemented in terms of select() or poll()...

If "ready" behaves like a select() that's been passed a timeval with zeros for tv_sec, and tv_usec, then the downside to "ready" is that you'll be spinning... Can you pass a timeout to ready?

dicroce
The `ready?` method appears to be entirely non-blocking, I'm assuming that underneath it all, ruby is buffering its sockets in some capacity, or it's behaving like `select` with a very short timeout.
Ian
Ian
Based on diving more into the behavior of ioctl with FIONREAD, I believe you are correct that so long as the IO (TCPSocket in my case) object supports the call, this is safe to use with "higher level" read methods. Thanks for the insight.
Ian