Skip to content

Instantly share code, notes, and snippets.

@awfulcooking
Created May 7, 2023 18:03
Show Gist options
  • Save awfulcooking/2bb0e04b5316f80436dabffca8da6811 to your computer and use it in GitHub Desktop.
Save awfulcooking/2bb0e04b5316f80436dabffca8da6811 to your computer and use it in GitHub Desktop.
module ReadlineNonblock
def readline_nonblock
@readline_nonblock_buf ||= ""
until @readline_nonblock_buf[-1] == "\n"
# read_nonblock may raise e.g. IO::WaitReadable, or EOFError
@readline_nonblock_buf << read_nonblock(1)
end
complete_line = @readline_nonblock_buf
@readline_nonblock_buf = ""
return complete_line
end
end
::IO.include ReadlineNonblock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment