MultiIO
is a Ruby IO
object that
is composed of other IO objects. It is meant to be used as a drop-in replacement for
regular IO objects like files, sockets, stdout/stderr streams etc.
This is useful, for example, when one wants to duplicate writes to multiple destinations (e.g. standard output, a file and a socket). Similarly, it can be used to read the concatenation of multiple IO sources.
Assuming we want to duplicate writes to a string buffer, stdout and a file:
> require "stringio"
> str = StringIO.new
> io = MultiIO.new(str, $stdout, File.new("foo", "w"))
> # write a message to all underlying IO objects (stdout is printed immediately
> # since it's attached to the terminal)
> io.puts "bar"
bar
> io.flush
> str.string # => "bar\n"
> File.read("foo") # => "bar\n"
This is an alpha release. Not all methods from IO are implemented yet so it can't be used in all cases as a drop-in replacement for regular IO objects.
The goal is to eventually implement the complete IO
interface.
IO methods implemented:
#close
#flush
#puts
#read
#rewind
#write
Feel free to submit a patch if you need something that's missing.
Running the tests:
$ bundle exec rake
MultiIO is licensed under MIT. See LICENSE.