Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 23, 2025 04:18
An example refinement method like `IO.pipe`'s block form but with no buffering.
class IO
module UnbufferedPipe
refine IO.superclass do
def unbuffered_pipe(*, **)
IO.pipe(*, **) do |read_io, write_io|
read_io.sync = true
write_io.sync = true
yield read_io, write_io
end
end
end
end
end
##
# Usage example
using IO::UnbufferedPipe
IO.unbuffered_pipe do |read_io, write_io|
write_io << "hola\n"
read_io.gets
end
#=> "hola\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment