Awesome. Now, can we break this with threads? Aredridel?
Of course, I wonder if IO#reopen is thread safe anyway, regardless of
the block. I've never seriously tested it.
This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.
From: Stephen Veit [mailto:firstname.lastname@example.org]
Sent: Friday, August 25, 2006 3:32 PM
To: ruby-talk ML
Subject: Re: Can you temporarily turn off STDERR ??
On 8/25/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> This comes up often enough that I wonder if allowing
IO#reopen to take
> a block would be a good idea:
> $stderr.reopen('/dev/null') do
> # Code here redirects to /dev/null
> # Code outside the block behaves normally
> I thought this idea had been brought up before, and that there was
> some downside to this, but I can't find any references to it now.
> - Dan
How about this?
alias_method :_orig_reopen, :reopen
is_path = args.length == 2
path_or_io, mode_str = args
old_io = clone
path_or_io = File.open(path_or_io, mode_str || "w") if is_path
path_or_io.close if is_path
if $0 == __FILE__
$stderr.reopen('error_log', 'w') do
$stderr.puts "Stuff on error"
puts "normal stuff"
File.open('error_log_2', "w") do |file|
$stderr.puts "Stuff on error 2"
puts "normal stuff 2"
$stderr.puts "Stuff on error 3"
puts "normal stuff 3"
# Make sure old way works
old_stderr = $stderr.clone
File.open('error_log_4', 'w') do |log_file|
$stderr.puts "Stuff on error 4"
puts "normal stuff 4"
$stderr.reopen('error_log_5', 'w') do
$stderr.puts "Stuff on error 5"
puts "normal stuff 5"
raise "This message courtesy of raise"
rescue => e