I can't seem to find Ruby's version of dup2 and friends.
How does one redirect output in Ruby?
I'm trying to write something along the lines of
File.with_output_to_file ("foobar", "w") {
puts "Hello"
puts "World"
system("ls")
}
and want all output sent to "foobar". Typically one uses 'dup2' for this.
but I can't seem to find it in Ruby.
class File
def File.with_output_to_file (fname, mode="w", &blk)
begin
dout = $defout
$defout = File.open fname, mode
yield
ensure
$defout.close
$defout = dout
end
end
end
is a first approximation taking care of #puts and family but not output of
child processes.
Are dup2 and friends yet to be added to Ruby, or am I missing something?
Thanks,
Raja