From 8754ee6d867ab5a27411f5b1a1b57cb646b48bd1 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Fri, 28 Aug 2026 01:34:06 +0300 Subject: [PATCH 1/2] Preserve headers when copying empty CSV tables into another access mode --- lib/csv/table.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/csv/table.rb b/lib/csv/table.rb index fb19f545..6da30e60 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: From 71ce191118d51a2009047b4975432010e9b81de2 Mon Sep 17 00:00:00 2001 From: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Date: Fri, 28 Aug 2026 14:32:00 +0300 Subject: [PATCH 2/2] Add regression coverage for header-only table copies --- test/csv/test_table.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/csv/test_table.rb b/test/csv/test_table.rb index e8ab7404..927f0506 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