Created
December 20, 2015 05:01
-
-
Save kwatch/22f09c5391b3e2990435 to your computer and use it in GitHub Desktop.
My favorite nonblocking read() API in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IO | |
## | |
## Easy-to-use nonblocking read() | |
## | |
## * returns non-empty string when data exists | |
## * returns empty string when data not exist | |
## * returns nil when EOF | |
## | |
def read_nonblocking(size) | |
return read_nonblock(size) | |
rescue IO::EAGAINWaitReadable | |
return "" | |
rescue EOFError | |
return nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment