I want to make a method that writes some CSV output to a filename if given and stdout if not given.
It seems like I need to treat my calls to CSV
differently depending on if it's a file or to stdout, but I'd really like to treat the output stream z
as something I can write to and not have to whether it's a file on disk or the stdout stream.
Is this possible?
Below is my attempt and errors:
require 'csv'
require 'pathname'
require 'csv'
require 'pathname'
def write_to_csv_or_stdout foo, bar, z=nil
z = Pathname.new(z) if z
z ||= $stdout
res = [[foo, bar, "baz"]]
CSV(z) do |csv|
res.each do |row|
csv << row
end
end
end
write_to_csv_or_stdout "foo", "bar"
# foo
# bar
#=> foo,bar,baz
# write_to_csv_or_stdout "foo", "bar", "baz"
# (NoMethodError)