From bcadde34bc3e88f1741d3b98008d52778c109467 Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Wed, 17 Oct 2018 16:41:35 +0500 Subject: [PATCH] fixes #389 #to_csv: changed predefined argument and add documentation --- README.md | 9 +++++++++ lib/roo/formatters/csv.rb | 2 +- spec/lib/roo/base_spec.rb | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 639a2daa..d2442f25 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,15 @@ sheet.to_xml sheet.to_yaml ``` +You can specified file as default argument to `#to_csv`: + + # puts csv format content into a specified file + sheet.to_csv(File.new("/dev/null")) + +also you can specify the custom separator for the content: + + sheet.to_csv(separator: ":") # default separator is comma: "," + ### Excel (xlsx and xlsm) Support Stream rows from an Excelx spreadsheet. diff --git a/lib/roo/formatters/csv.rb b/lib/roo/formatters/csv.rb index 3181f091..1c30c07a 100644 --- a/lib/roo/formatters/csv.rb +++ b/lib/roo/formatters/csv.rb @@ -1,7 +1,7 @@ module Roo module Formatters module CSV - def to_csv(filename = nil, separator = ",", sheet = default_sheet) + def to_csv(filename = nil, sheet = default_sheet, separator: ",") if filename File.open(filename, "w") do |file| write_csv_content(file, sheet, separator) diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index 76cefcc4..4cdbc01e 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -269,7 +269,7 @@ def sheets end it 'should convert the spreadsheet to csv using the separator when is passed on the parameter' do - expect(spreadsheet.to_csv(nil, ';')).to eq(expected_csv_with_semicolons) + expect(spreadsheet.to_csv(separator: ';')).to eq(expected_csv_with_semicolons) end end end