
Untitled
By: a guest on
Aug 21st, 2012 | syntax:
None | size: 0.68 KB | hits: 15 | expires: Never
require 'stringio'
old_out = $stdout
class TeeIO < StringIO
def initialize(stdout)
super()
@stdout = stdout
end
def write(*args)
super
@stdout.write(*args)
end
end
class ProxyIO < BasicObject
def initialize(*streams)
@streams = streams
end
private
def method_missing(name, *args, &block)
result = nil
@streams.each do |stream|
result = stream.send(name, *args, &block)
end
result
end
end
$stdout = tee = TeeIO.new(old_out)
puts "hello world!"
$stdout = old_out
puts "From tee:"
puts tee.string
$stdout = ProxyIO.new(old_out, stringio = StringIO.new)
puts "hello again!"
$stdout = old_out
puts "From proxy:"
puts stringio.string