# Ignore printout

**URL:** <https://www.ruby-forum.com/t/ignore-printout/117021>\
**Category:** Ruby\
**Created:** [October 12, 2007, 4:40am UTC](https://www.ruby-forum.com/t/ignore-printout/117021 "2007-10-12T04:40:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 4:40am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/1 "2007-10-12T04:40:57Z")

</div>

I am using a compiled C-code in my Ruby program. The problem is that  
the C-code prints some messages that I don’t want to show in my Ruby  
program. What would be the best way to get rid of these printouts?

I don’t want to get into the original C-code and take away the print  
commands since that is not my code. Are the printouts sent directly to  
standard output, meaning there is no way for Ruby to interfere?

Thanks for any help!

/Fredrik

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 5:55am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/2 "2007-10-12T05:55:44Z")

</div>

Fredrik wrote:

> I am using a compiled C-code in my Ruby program. The problem is that  
> the C-code prints some messages that I don’t want to show in my Ruby  
> program. What would be the best way to get rid of these printouts?
> 
> I don’t want to get into the original C-code and take away the print  
> commands since that is not my code. Are the printouts sent directly to  
> standard output, meaning there is no way for Ruby to interfere?

You may try to redirect $stdout while the C code runs - I’m not sure  
this will work, but it’s worth a try.

mortee

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 6:18am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/3 "2007-10-12T06:18:31Z")

</div>

On Oct 11, 2007, at 19:40 , Fredrik wrote:

> I am using a compiled C-code in my Ruby program. The problem is that  
> the C-code prints some messages that I don’t want to show in my Ruby  
> program. What would be the best way to get rid of these printouts?
> 
> I don’t want to get into the original C-code and take away the print  
> commands since that is not my code. Are the printouts sent directly to  
> standard output, meaning there is no way for Ruby to interfere?

You will need to reopen STDOUT to /dev/null

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 8:05am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/4 "2007-10-12T08:05:27Z")

</div>

I should clarify myself:  
I was expecting to find that STDOUT is a constant so that I could do:

$stdout.reopen(File.new(’/dev/null’,‘w’))  
puts ‘Nobody can see this.’  
$stdout.reopen(STDOUT)

But after the first line, my STDOUT is pointing to the exact same  
place as $stdout. Isn’t that weird?

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 8:50am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/5 "2007-10-12T08:50:31Z")

</div>

Well, this does the trick (note the ‘clone’ instruction on line 1). It  
seems to be working nicely also for the compiled binary that I am  
calling.

out = $stdout.clone  
$stdout.reopen(File.new(’/dev/null’,‘w’))  
puts ‘You cant see this!’  
$stdout.reopen(out)

I am still confused over why STDOUT is not a constant though…  
Thanks for your pointers!

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 7:48am UTC](https://www.ruby-forum.com/t/ignore-printout/117021/6 "2007-10-12T07:48:31Z")

</div>

> You will need to reopen STDOUT to /dev/null

Then I have two more questions to that 🙂

1. What’s the difference between $stdout and STDOUT? They seem to be  
the same thing.

2. In my pickaxe book I read that “Assignment to $stdout is  
depracated; use $stdout.reopen instead.” But if I want to redirect  
$stdout and then $direct it back to what it was before, surely I HAVE  
to use assignment to $stdout. Like this:

out = $stdout  
$stdout = File.new(’/dev/null’,‘w’)  
puts ‘This is not to be seen by anybody.’  
$stdout.reopen(out)

If I use reopen on the second line instead, the ‘out’ variable comes  
along to this new value and nobody knows where the “usual stdout” is,  
right?

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/ignore-printout/117021/7 "2007-10-12T14:42:40Z")

</div>

Fredrik wrote:

> out = $stdout  
> $stdout = File.new(’/dev/null’,‘w’)  
> puts ‘This is not to be seen by anybody.’  
> $stdout.reopen(out)

Only as an remark - on Windows you should use

$stdout = File.open(“nul:”, “w”)

to ignore Output, and

$stdout = STDOUT

to reset.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/ignore-printout/117021/8 "2007-10-12T12:44:04Z")

</div>

Fredrik wrote:

> I am still confused over why STDOUT is not a constant though…  
> Thanks for your pointers!

I’d guess that when you reopen a stream, then it as an object remains  
the same, just what you send it will end up somewhere else than before.  
In contrast, when you assign to a variable, the object itself changes  
which that variable references.

Since $stdout and STDOUT initially point to the same object, if you dont  
assign $stdout, instead you modify the onject itself, the two continue  
to reference the same original object. Remember that you can modify a  
constant object’s state without any warning anyway, so you could even  
reopen STDOUT itself.

mortee

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [October 12, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/ignore-printout/117021/9 "2007-10-12T18:45:06Z")

</div>

On Oct 11, 2007, at 22:45 , Fredrik wrote:

> > You will need to reopen STDOUT to /dev/null
> 
> Then I have two more questions to that 🙂
> 
> 1. What’s the difference between $stdout and STDOUT? They seem to be  
> the same thing.

[http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)

In your case you want to reopen STDOUT because somebody using your  
code may have changed $stdout, if they want to capture or redirect  
output.

> 1. In my pickaxe book I read that “Assignment to $stdout is  
> depracated; use $stdout.reopen instead.” But if I want to redirect  
> $stdout and then $direct it back to what it was before, surely I HAVE  
> to use assignment to $stdout. Like this:

I don’t think this is true anymore. You can’t reopen using StringIO  
to capture regular ruby puts or p.

> out = $stdout  
> $stdout = File.new(‘/dev/null’,‘w’)  
> puts ‘This is not to be seen by anybody.’  
> $stdout.reopen(out)
> 
> If I use reopen on the second line instead, the ‘out’ variable comes  
> along to this new value and nobody knows where the “usual stdout” is,  
> right?

Right, you should dup $stdout there.
