Using Ruby's "ready?" IO method with gets, puts, etc
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, surprising开发者_开发技巧ly 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.
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?
精彩评论