From b730a4cdbcb51eca6f4b8b5debf920245c8c33df Mon Sep 17 00:00:00 2001 From: ericgpks Date: Sat, 6 Aug 2022 23:29:26 +0900 Subject: [PATCH 1/5] feat: add generate_lines method --- lib/csv.rb | 40 ++++++++++++++++++++++++++++++++ test/csv/interface/test_write.rb | 9 +++++++ 2 files changed, 49 insertions(+) diff --git a/lib/csv.rb b/lib/csv.rb index 1635bda0..50f9a022 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1464,6 +1464,46 @@ def generate_line(row, **options) (new(str, **options) << row).string end + # :call-seq: + # CSV.generate_lines(rows) + # CSV.generate_lines(rows, **options) + # + # Returns the \String created by generating \CSV from + # using the specified +options+. + # + # Argument + # + # Special options: + # * Option :row_sep defaults to "\n"> on Ruby 3.0 or later + # and $INPUT_RECORD_SEPARATOR ($/) otherwise.: + # $INPUT_RECORD_SEPARATOR # => "\n" + # * This method accepts an additional option, :encoding, which sets the base + # Encoding for the output. This method will try to guess your Encoding from + # the first non-+nil+ field in +row+, if possible, but you may need to use + # this parameter as a backup plan. + # + # For other +options+, + # see {Options for Generating}[#class-CSV-label-Options+for+Generating]. + # + # --- + # + # Returns the \String generated from an + # CSV.generate_lines(['foo', '0'],['bar', '1'],['baz', '2']) # => "foo,0\r\nbar,1\r|nbaz.2" + # + # --- + # + # Raises an exception + # # Raises NoMethodError (undefined method `find' for :foo:Symbol) + # CSV.generate_lines(:foo) + # + def generate_lines(rows, **options) + self.generate(**options) do |csv| + rows.each do |row| + csv << row + end + end + end + # # :call-seq: # open(file_path, mode = "rb", **options ) -> new_csv diff --git a/test/csv/interface/test_write.rb b/test/csv/interface/test_write.rb index 02c2c5c5..f149f1c6 100644 --- a/test/csv/interface/test_write.rb +++ b/test/csv/interface/test_write.rb @@ -78,6 +78,15 @@ def test_generate_line_row_sep LINE end + def test_generate_lines + lines = CSV.generate_lines([["foo", "bar"], [1, 2], [3, 4]]) + assert_equal(<<-LINES, lines) +foo,bar +1,2 +3,4 + LINES + end + def test_generate_line_shortcut line = ["1", "2", "3"].to_csv(col_sep: ";") assert_equal(<<-LINE, line) From 34b2af051fd1a36b3a008a32e1318dba71e6615d Mon Sep 17 00:00:00 2001 From: ericgpks Date: Wed, 10 Aug 2022 17:07:16 +0900 Subject: [PATCH 2/5] fix: add description about argument --- lib/csv.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/csv.rb b/lib/csv.rb index 50f9a022..1b8a8b85 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1471,7 +1471,7 @@ def generate_line(row, **options) # Returns the \String created by generating \CSV from # using the specified +options+. # - # Argument + # Argument +rows+ must be an \Array of row. Row is \Array of \String or \CSV::Row. # # Special options: # * Option :row_sep defaults to "\n"> on Ruby 3.0 or later From 15e1f4c0ec85a3a4d6baae181a3ee37f93a0f805 Mon Sep 17 00:00:00 2001 From: Eriko Sugiyama <40660382+ericgpks@users.noreply.github.com> Date: Tue, 23 Aug 2022 18:43:44 +0900 Subject: [PATCH 3/5] fix end tag for doc Co-authored-by: Sutou Kouhei --- lib/csv.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/csv.rb b/lib/csv.rb index 1b8a8b85..961f022e 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1474,7 +1474,7 @@ def generate_line(row, **options) # Argument +rows+ must be an \Array of row. Row is \Array of \String or \CSV::Row. # # Special options: - # * Option :row_sep defaults to "\n"> on Ruby 3.0 or later + # * Option :row_sep defaults to "\n" on Ruby 3.0 or later # and $INPUT_RECORD_SEPARATOR ($/) otherwise.: # $INPUT_RECORD_SEPARATOR # => "\n" # * This method accepts an additional option, :encoding, which sets the base From c8630dac4d62ce79307fa5135412500c0bcccc9e Mon Sep 17 00:00:00 2001 From: ericgpks Date: Tue, 23 Aug 2022 18:47:05 +0900 Subject: [PATCH 4/5] fix: change position of a test --- test/csv/interface/test_write.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/csv/interface/test_write.rb b/test/csv/interface/test_write.rb index f149f1c6..0cd39a76 100644 --- a/test/csv/interface/test_write.rb +++ b/test/csv/interface/test_write.rb @@ -78,6 +78,13 @@ def test_generate_line_row_sep LINE end + def test_generate_line_shortcut + line = ["1", "2", "3"].to_csv(col_sep: ";") + assert_equal(<<-LINE, line) +1;2;3 + LINE + end + def test_generate_lines lines = CSV.generate_lines([["foo", "bar"], [1, 2], [3, 4]]) assert_equal(<<-LINES, lines) @@ -87,13 +94,6 @@ def test_generate_lines LINES end - def test_generate_line_shortcut - line = ["1", "2", "3"].to_csv(col_sep: ";") - assert_equal(<<-LINE, line) -1;2;3 - LINE - end - def test_headers_detection headers = ["a", "b", "c"] CSV.open(@output.path, "w", headers: true) do |csv| From 4dad5d1a6d78d3eef4b8cc77f5d0d720f9d8c487 Mon Sep 17 00:00:00 2001 From: ericgpks Date: Tue, 23 Aug 2022 18:55:18 +0900 Subject: [PATCH 5/5] fix: change document separator --- lib/csv.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/csv.rb b/lib/csv.rb index 961f022e..b7cb24de 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1488,7 +1488,7 @@ def generate_line(row, **options) # --- # # Returns the \String generated from an - # CSV.generate_lines(['foo', '0'],['bar', '1'],['baz', '2']) # => "foo,0\r\nbar,1\r|nbaz.2" + # CSV.generate_lines(['foo', '0'], ['bar', '1'], ['baz', '2']) # => "foo,0\nbar,1\nbaz.2\n" # # --- #