Skip to content

Commit

Permalink
Adding support for :write_headers => false to Table.to_csv().
Browse files Browse the repository at this point in the history
  • Loading branch information
JEG2 committed Feb 19, 2010
1 parent cb34834 commit 529effc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -5,6 +5,7 @@ Below is a complete listing of changes for each revision of FasterCSV.
== 1.5.2

* A bug fix to allow IO Exceptions to reach the calling code from Moses Hohman.
* Added support for <tt>:write_headers => false</tt> to Table.to_csv().

== 1.5.1

Expand Down
6 changes: 5 additions & 1 deletion lib/faster_csv.rb
Expand Up @@ -704,8 +704,12 @@ def to_a
# Returns the table as a complete CSV String. Headers will be listed first,
# then all of the field rows.
#
# This method assumes you want the Table.headers(), unless you explicitly
# pass <tt>:write_headers => false</tt>.
#
def to_csv(options = Hash.new)
@table.inject([headers.to_csv(options)]) do |rows, row|
wh = options.fetch(:write_headers, true)
@table.inject(wh ? [headers.to_csv(options)] : [ ]) do |rows, row|
if row.header_row?
rows
else
Expand Down
2 changes: 2 additions & 0 deletions test/tc_table.rb
Expand Up @@ -251,6 +251,8 @@ def test_to_csv
# with options
assert_equal( csv.gsub(",", "|").gsub("\n", "\r\n"),
@table.to_csv(:col_sep => "|", :row_sep => "\r\n") )
assert_equal( csv.to_a[1..-1].join,
@table.to_csv(:write_headers => false) )

# with headers
assert_equal(csv, @header_table.to_csv)
Expand Down

0 comments on commit 529effc

Please sign in to comment.