[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

redirect stdout

T. Onoma

11/24/2003 12:48:00 PM

I need to temporarily redirect standard output to nowhere. Not sure how to do.

Suggestions?

Thanks,
-t0



8 Answers

Reimer Behrends

11/24/2003 3:49:00 PM

0

T. Onoma (transami@runbox.com) wrote:
> I need to temporarily redirect standard output to nowhere. Not sure
> how to do.

Assuming you run some *IX:

STDOUT.reopen "/dev/null", "w"

will redirect the standard output (including not just Ruby output,
but even the output of subprocesses) to /dev/null.

Reimer Behrends

Mark J. Reed

11/24/2003 4:37:00 PM

0

On Mon, Nov 24, 2003 at 03:49:21PM +0000, Reimer Behrends wrote:
> T. Onoma (transami@runbox.com) wrote:
> > I need to temporarily redirect standard output to nowhere. Not sure
> > how to do.
>
> Assuming you run some *IX:
>
> STDOUT.reopen "/dev/null", "w"
>
> will redirect the standard output (including not just Ruby output,
> but even the output of subprocesses) to /dev/null.

But it also loses the original STDOUT, and M Onoma did say that the
redirection was to be temporary. You can accomplish that by saving
the original value and restoring it later, like this:

old_stdout = STDOUT.dup
STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
STDOUT.reopen(old_stdout)

Or, if you aren't invoking any processes external to ruby, you can leave
STDOUT alone and just change $stdout:

$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
$stdout = STDOUT

-Mark

Mark J. Reed

11/24/2003 4:41:00 PM

0

Minor correction: forgot the mode on my open calls.
Add , "w" to both of the below:

On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:
> STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )

> $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )

-Mark

T. Onoma

11/24/2003 5:37:00 PM

0

On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:
> Minor correction: forgot the mode on my open calls.
> Add , "w" to both of the below:
>
> On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:
> > STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
>
> STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )
>
> > $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
>
> $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )
>
> -Mark

Perfecto!

You are an astute reader, Mark. :)

Thank you,
-T0




Eric Schwartz

11/24/2003 7:27:00 PM

0

"Mark J. Reed" <markjreed@mail.com> writes:
> Or, if you aren't invoking any processes external to ruby, you can leave
> STDOUT alone and just change $stdout:

Okay, this hints at the answer to a question I've been wondering about
for a while now: what's the difference between STDOUT/STDERR/STDIN and
$stdout/$stderr/$stdin?

It seems your message implies that STDOUT/STDERR/STDIN are IO objects
that when changed, the changes are passed on to children, but if you
change $stdout and friends, those changes are not passed on to
children.

If so, this confuses me greatly, since $stdout prints to stdout, and
if open filehandles (including the magic 3) are inherited by children
(as they are on most *nices), then how can I reopen $stdout without
changing stdout? Why is there such a (to me) confusing and needless
distinction?

Of course, if I inferred incorrectly, disregard the previous
paragraph. :)

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.

Reimer Behrends

11/26/2003 12:32:00 AM

0

Mark J. Reed (markjreed@mail.com) wrote:
[...]
> But it also loses the original STDOUT, and M Onoma did say that the
> redirection was to be temporary.

Good catch. I missed that one.

> You can accomplish that by saving
> the original value and restoring it later, like this:
>
> old_stdout = STDOUT.dup
> STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> . . .
> STDOUT.reopen(old_stdout)

You need

old_stdout.close

here. Otherwise you have a file descriptor leak. Similarly:

> Or, if you aren't invoking any processes external to ruby, you can leave
> STDOUT alone and just change $stdout:
>
> $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> . . .
$stdout.close
> $stdout = STDOUT

(And, of course, add the "w" flag to the open call, as you had already
mentioned.)

Reimer Behrends

Mark J. Reed

11/26/2003 3:09:00 PM

0

On Wed, Nov 26, 2003 at 12:32:02AM +0000, Reimer Behrends wrote:
> Mark J. Reed (markjreed@mail.com) wrote:
> [...]
> > But it also loses the original STDOUT, and M Onoma did say that the
> > redirection was to be temporary.
>
> Good catch. I missed that one.

Right back at you for catching the fd leaks in my examples. Whups. :)

Thanks.

-Mark

T. Onoma

11/28/2003 7:31:00 AM

0

On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:
> Minor correction: forgot the mode on my open calls.
> Add , "w" to both of the below:
>
> On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:
> > STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
>
> STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )
>
> > $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
>
> $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null", "w" )
>
> -Mark

Thanks!

-t0