Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created December 20, 2015 05:01
Show Gist options
  • Save kwatch/22f09c5391b3e2990435 to your computer and use it in GitHub Desktop.
Save kwatch/22f09c5391b3e2990435 to your computer and use it in GitHub Desktop.
My favorite nonblocking read() API in Ruby
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