Bug #9988

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

Added by Jeremy Evans over 1 year ago. Updated over 1 year ago.

Assignee:James Gray
ruby -v:ruby -v: ruby 2.1.2p95 (2014-05-08 revision 45877) [i386-openbsd] Backport:2.0.0: UNKNOWN, 2.1: UNKNOWN


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 << []}"

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|}"

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

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


#1 Updated by Hiroshi SHIBATA over 1 year ago

  • Status changed from Open to Assigned

Also available in: Atom PDF