def redirect_io!(options = {})
stdin = options[:stdin] || @stdin_file
stdout = options[:stdout] || @stdout_file
stderr = options[:stderr] || @stderr_file
{
STDIN => stdin, STDOUT => stdout, STDERR => stderr
}.each do |io, file|
opened = false
fd =
case
when file.is_a?(IO)
file
when file.to_s == 'null'
opened = true
open('/dev/null', 'ab+')
else
opened = true
open(file, 'ab+')
end
begin
fd.sync = true rescue nil
fd.truncate(0) rescue nil
io.reopen(fd)
ensure
fd.close rescue nil if opened
end
end
end