Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby: open File object to stdout

Tags:

file

io

ruby

stdout

I am quite new with Ruby, and I am trying to open a File object that points to stdout.

I know from this question that redirecting stdout to point to a File is quite simple, but what about redirecting a File to point to stdout?

I am writing a program, and I am thinking of providing the users with the option to write part of the output to a file. If they do not choose that option, then all of the output should be written to stdout.

See this pseudocode:

if output redirect option is selected
    o = File.open('given filename','w')
else
    o = File.open($stdout, 'w')
end

Here is pseudocode for a possible usecase:

puts 'Generating report for XYZ'
report = ReportGenerator::generateReport('XYZ')
o.puts report

As you can see, I desire only o to put the report to stdout if the output redirection option was not specified. The 'Generating report' message, however, I need to still be printed to stdout, so redirecting stdout will be cumbersome, especially since I have many more messages and many more places in which I am (possibly) alternating between output streams.

o = File.open($stdout, 'w') is the part I am uncertain of.

like image 314
scottysseus Avatar asked Mar 16 '23 01:03

scottysseus


1 Answers

Ruby's $stdout is an IO instance that responds to puts, so you can just write:

if output_redirect_option_is_selected
  o = File.open('given filename','w')
else
  o = $stdout.dup
end

dup-ing $stdout allows you to close o without affecting $stdout:

o = $stdout.dup
o.close
puts 'bye' # works as expected

whereas:

o = $stdout
o.close
puts 'bye' # raises IOError
like image 155
Stefan Avatar answered Mar 17 '23 13:03

Stefan