# Re: Can you temporarily turn off STDERR?

**URL:** <https://www.ruby-forum.com/t/re-can-you-temporarily-turn-off-stderr/66499>\
**Category:** Ruby\
**Created:** [August 26, 2006, 11:58am UTC](https://www.ruby-forum.com/t/re-can-you-temporarily-turn-off-stderr/66499 "2006-08-26T11:58:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [August 26, 2006, 11:58am UTC](https://www.ruby-forum.com/t/re-can-you-temporarily-turn-off-stderr/66499/1 "2006-08-26T11:58:54Z")

</div>

> > 
> 
> can’t change
> 
> [http://codeforpeople.com/lib/ruby/session/session-2.4.0/README](http://codeforpeople.com/lib/ruby/session/session-2.4.0/README)  
> [http://codeforpeople.com/lib/ruby/open4/open4-0.5.1/README](http://codeforpeople.com/lib/ruby/open4/open4-0.5.1/README)
> 
> in particular the Open4::spawn command. both are available as

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

end

# 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.

Thoughts?

- Dan

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.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/re-can-you-temporarily-turn-off-stderr/66499/2 "2006-09-04T14:46:14Z")

</div>

On 8/25/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.
> 
> Thoughts?
> 
> - Dan

How about this?

#!/usr/bin/env ruby

# reopen\_with\_block.rb

class IO

alias\_method :\_orig\_reopen, :reopen

def reopen(\*args)  
if block\_given?  
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  
\_orig\_reopen(path\_or\_io)  
begin  
yield  
ensure  
\_orig\_reopen(old\_io)  
path\_or\_io.close if is\_path  
end  
else  
\_orig\_reopen(\*args)  
end  
end

end

if $0 == **FILE**  
$stderr.reopen(‘error\_log’, ‘w’) do  
$stderr.puts “Stuff on error”  
puts “normal stuff”  
end

File.open(‘error\_log\_2’, “w”) do |file|  
$stderr.reopen(file) do  
$stderr.puts “Stuff on error 2”  
puts “normal stuff 2”  
end  
end

$stderr.reopen(‘/dev/null’) do  
$stderr.puts “Stuff on error 3”  
puts “normal stuff 3”  
end

# Make sure old way works

old\_stderr = $stderr.clone  
File.open(‘error\_log\_4’, ‘w’) do |log\_file|  
$stderr.reopen(log\_file)  
begin  
$stderr.puts “Stuff on error 4”  
puts “normal stuff 4”  
ensure  
$stderr.reopen(old\_stderr)  
end  
end

begin  
$stderr.reopen(‘error\_log\_5’, ‘w’) do  
$stderr.puts “Stuff on error 5”  
puts “normal stuff 5”  
raise “This message courtesy of raise”  
end  
rescue =\> e  
puts e  
end

end

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/re-can-you-temporarily-turn-off-stderr/66499/3 "2006-09-04T14:46:14Z")

</div>

> downside to this, but I can’t find any references to it now.
> 
> Thoughts?

Can’t handle threading sanely, for one.
