ruby-changes:47524
From: hsbt <ko1@a...>
Date: Tue, 22 Aug 2017 15:17:48 +0900 (JST)
Subject: [ruby-changes:47524] hsbt:r59640 (trunk): Fixed to write_headers option behavior when given no rows.
hsbt 2017-08-22 15:17:42 +0900 (Tue, 22 Aug 2017) New Revision: 59640 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59640 Log: Fixed to write_headers option behavior when given no rows. [Bug #9988][ruby-core:63375] Modified files: trunk/lib/csv.rb trunk/test/csv/test_interface.rb Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 59639) +++ lib/csv.rb (revision 59640) @@ -1561,6 +1561,12 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1561 # track our own lineno since IO gets confused about line-ends is CSV fields @lineno = 0 + + # make sure headers have been assigned + if header_row? and [Array, String].include? @use_headers.class and @write_headers + parse_headers # won't read data for Array or String + self << @headers + end end # @@ -1677,9 +1683,8 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1683 # def <<(row) # make sure headers have been assigned - if header_row? and [Array, String].include? @use_headers.class + if header_row? and [Array, String].include? @use_headers.class and !@write_headers parse_headers # won't read data for Array or String - self << @headers if @write_headers end # handle CSV::Row objects and Hashes Index: test/csv/test_interface.rb =================================================================== --- test/csv/test_interface.rb (revision 59639) +++ test/csv/test_interface.rb (revision 59640) @@ -295,6 +295,19 @@ class TestCSV::Interface < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_interface.rb#L295 end end + def test_write_headers_empty + File.unlink(@path) + + CSV.open( @path, "wb", headers: "b|a|c", + write_headers: true, + col_sep: "|" ) do |csv| + end + + File.open(@path, "rb") do |f| + assert_equal("b|a|c", f.gets.strip) + end + end + def test_append # aliased add_row() and puts() File.unlink(@path) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/