| Class | IORedirect |
| In: |
lib/more/facets/ioredirect.rb
|
| Parent: | Object |
A class to redirect $stdout, or other IO object, to a StringIO object, or any other object with a write() method.
s = StringIO.new
r = Redirector.redirect($stdout, s) do
$stdout.puts "this is a test"
end
An exception-safe class method for redirection
# File lib/more/facets/ioredirect.rb, line 80 def self.redirect(from, to) s = self.new(from, to) begin yield ensure s.stop end end
Start redirection, if it has not already been started.
# File lib/more/facets/ioredirect.rb, line 55 def start raise "Redirection already in progress" if @t tmp = @from.dup r, w = IO.pipe @from.reopen(w) @t = Thread.new do begin loop do s = r.read(1) # TODO: can I make this buffered? @to.write(s) end ensure @from.reopen(tmp) @t = nil end end end