Project

General

Profile

Actions

Bug #9988

closed

CSV does not write headers when :write_headers is true and no rows are added

Added by jeremyevans0 (Jeremy Evans) almost 10 years ago. Updated over 6 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby -v: ruby 2.1.2p95 (2014-05-08 revision 45877) [i386-openbsd]
[ruby-core:63375]

Description

When the CSV :write_headers option is used, headers are only added if at least one row is added:

$ ruby -r csv -e "p CSV.generate(:headers=>%w'a b c', :write_headers=>true){|csv|}"
""
$ ruby -r csv -e "p CSV.generate(:headers=>%w'a b c', :write_headers=>true){|csv| csv << []}"
"a,b,c\n\n"

This seems like a bug to me. It should write the headers even if there are no rows. The attached patch does that:

$ ruby -I lib -r csv -e "p CSV.generate(:headers=>%w'a b c', :write_headers=>true){|csv|}"
"a,b,c\n"

The attached patch should probably be refactored to avoid the duplication, but I think that is best left to the csv maintainer.


Files

csv-write-headers-empty-fix.diff (1.47 KB) csv-write-headers-empty-fix.diff jeremyevans0 (Jeremy Evans), 06/27/2014 05:37 PM

Updated by hsbt (Hiroshi SHIBATA) over 9 years ago

  • Status changed from Open to Assigned

Updated by hsbt (Hiroshi SHIBATA) almost 7 years ago

  • Assignee changed from JEG2 (James Gray) to hsbt (Hiroshi SHIBATA)
Actions #3

Updated by hsbt (Hiroshi SHIBATA) over 6 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r59640.


Fixed to write_headers option behavior when given no rows.

[Bug #9988][ruby-core:63375]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0