https://github.com/agis/multi_io
A Ruby IO object that can be composed of other IO objects
https://github.com/agis/multi_io
io ruby ruby-gem ruby-io ruby-library
Last synced: 9 months ago
JSON representation
A Ruby IO object that can be composed of other IO objects
- Host: GitHub
- URL: https://github.com/agis/multi_io
- Owner: agis
- License: mit
- Created: 2020-04-30T19:00:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-30T00:20:28.000Z (over 5 years ago)
- Last Synced: 2025-03-29T05:41:33.588Z (10 months ago)
- Topics: io, ruby, ruby-gem, ruby-io, ruby-library
- Language: Ruby
- Homepage: https://rubygems.org/gems/multi_io
- Size: 16.6 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
multi_io
==============================================================================
[](https://badge.fury.io/rb/multi_io)
[](https://travis-ci.org/agis/multi_io)
[](LICENSE)
`MultiIO` is a Ruby [`IO`](https://ruby-doc.org/core-2.7.1/IO.html) 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.
Usage
------------------------------------------------------------------------------
Assuming we want to duplicate writes to a string buffer, stdout and a file:
```ruby
> 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"
```
Status
------------------------------------------------------------------------------
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`](https://ruby-doc.org/core-2.7.1/IO.html)
interface.
IO methods implemented:
- `#close`
- `#flush`
- `#puts`
- `#read`
- `#rewind`
- `#write`
Feel free to submit a patch if you need something that's missing.
Development
------------------------------------------------------------------------------
Running the tests:
```shell
$ bundle exec rake
```
License
--------------------------------------------------------------------------
MultiIO is licensed under MIT. See [LICENSE](LICENSE).