diff --git a/lib/csv/table.rb b/lib/csv/table.rb index fb19f54..6da30e6 100644 --- a/lib/csv/table.rb +++ b/lib/csv/table.rb @@ -240,7 +240,7 @@ def initialize(array_of_rows, headers: nil) # # Also note that changes to the duplicate table will not affect the original. def by_col - self.class.new(@table.dup).by_col! + self.class.new(@table.dup, headers: headers).by_col! end # :call-seq: @@ -278,7 +278,7 @@ def by_col! # # Also note that changes to the duplicate table will not affect the original. def by_col_or_row - self.class.new(@table.dup).by_col_or_row! + self.class.new(@table.dup, headers: headers).by_col_or_row! end # :call-seq: @@ -316,7 +316,7 @@ def by_col_or_row! # # Also note that changes to the duplicate table will not affect the original. def by_row - self.class.new(@table.dup).by_row! + self.class.new(@table.dup, headers: headers).by_row! end # :call-seq: diff --git a/test/csv/test_table.rb b/test/csv/test_table.rb index e8ab740..927f050 100644 --- a/test/csv/test_table.rb +++ b/test/csv/test_table.rb @@ -51,6 +51,13 @@ def test_modes assert_equal(:col_or_row, @table.mode) end + def test_copy_modes_preserve_header_only_table_headers + [:by_col, :by_row, :by_col_or_row].each do |mode| + copied = @header_only_table.public_send(mode) + assert_equal(%w[A B C], copied.headers, mode) + end + end + def test_headers assert_equal(@rows.first.headers, @table.headers) end