I've done quite a bit of reading about socket programming in Ruby, but
most of the examples show simple cases of accepting a socket
connection, spitting out some fixed bit of data, and then closing.
What about socket connections where you can't predict the length or
timing of data that you'll receive?  None of the available web pages
give any hint of how to work interactively with sockets.

Here are a few things that I was hoping maybe some knowledgable people
could help me figure out how to do with sockets in Ruby:

- Block until 1 or more bytes have been received, and then return as
many as have arrived (even if fewer than some buffer size)

- Block until a fixed number of bytes have been received, or a time-
out has elapsed

- Check how many bytes have been received without blocking

- Detect when the other end has closed the socket

- Distinguish between a timeout and a closed socket


Thank you for your help!