From 9dc1ac390202e4212192e9a80f6d4b67313d6bf1 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 arguments and add documentation --- README.md | 13 +++++++++++++ lib/roo/formatters/csv.rb | 2 +- spec/lib/roo/base_spec.rb | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 639a2daa..08177e89 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,19 @@ sheet.to_xml sheet.to_yaml ``` +You can specified file as default argument to `#to_csv`: + +```ruby +# 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: + +```ruby +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..595a6095 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, separator: ",", sheet: default_sheet) 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