Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting STDOUT To A File And Back Again

Tags:

ruby

I routed STDOUT to a file using this code:

STDOUT.reopen(File.open("./OUTPUT",'w+'))

Now I need to route STDOUT to the terminal again.

How would I do this?

like image 885
RyanScottLewis Avatar asked Sep 24 '09 08:09

RyanScottLewis


2 Answers

UPDATED

orig_std_out = STDOUT.clone
STDOUT.reopen(File.open('OUTPUT', 'w+'))
puts "test to file"
STDOUT.reopen(orig_std_out)
puts "test to screen"
like image 191
khelll Avatar answered Nov 15 '22 23:11

khelll


You need to reopen STDOUT on file handle 1, which is the standard fd handle for stdout (0=stdin, 1=stdout, 2=stderr).

I'm not a Ruby guy, but I think this is about right:

STDOUT.reopen(IO.for_fd(1, "r"))
like image 32
gavinb Avatar answered Nov 15 '22 21:11

gavinb