A class like tee(1) for Ruby.
Add this line to your application's Gemfile:
gem 'tee'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tee
Tee.open('a.txt') do |tee|
tee.puts 'foo'
end
# a.txt
foo
# STDOUT
foo
Tee.open('a.txt', 'b.txt') do |tee|
tee.puts 'bar'
end
# a.txt
bar
# b.txt
bar
# STDOUT
bar
Tee.open('a.txt', mode: 'a') do |tee|
tee.puts 'baz'
end
# a.txt
bar
baz
# STDOUT
baz
Tee.open('a.txt', 'b.txt', stdout: nil) do |tee|
tee.puts 'qux'
end
# a.txt
qux
# b.txt
qux
require 'stringio'
stringio = StringIO.new
open('a.txt', 'w') do |file|
Tee.open(file, stringio) do |tee|
tee.puts 'quux'
end # `file` doesn't close because it wasn't opened by Tee.
file.puts 'corge'
end
puts stringio.string
# a.txt
quux
corge
# STDOUT
quux
quux
Copyright (c) 2012 Masaki Takeuchi. See LICENSE for details.