ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
182 stars 114 forks source link

Add CSV::TSV class for tab-separated values #319

Closed jsxs0 closed 3 days ago

jsxs0 commented 1 month ago

Add TSV class for tab-separated files support

This PR adds a lightweight TSV class that provides first-class support for tab-separated files through a simple inheritance mechanism from the CSV class.

Implementation

The implementation has been simplified to only override the initialize method, which sets the default column separator to tab (\t). This minimalist approach maintains full compatibility with CSV while providing convenient TSV handling:

class TSV < CSV
  def initialize(data, **options)
    super(data, **({col_sep: "\t"}.merge(options)))
  end
end

Features

Example Usage

# Read TSV file with default tab separator
TSV.read("data.tsv")

# Parse TSV string
TSV.parse("a\tb\tc")

# Generate TSV content
TSV.generate do |tsv|
  tsv << ["a", "b", "c"]
  tsv << [1, 2, 3]
end

# Override separator if needed (though you might want to use CSV directly in this case)
TSV.read("data.csv", col_sep: ",")

Motivation

This change is motivated by:

(https://github.com/ruby/csv/issues/272)

Changes from Previous Version

The implementation has been significantly simplified:

kou commented 4 weeks ago

Hmm. Do we need to override all class methods? Can we just override TSV#initialize?

diff --git a/lib/csv.rb b/lib/csv.rb
index f6eb32a..8c71b4d 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -2979,6 +2979,12 @@ class CSV
   end
 end

+class TSV < CSV
+  def initialize(data, **options)
+    super(data, **({col_sep: "\t"}.merge(options)))
+  end
+end
+
 # Passes +args+ to CSV::instance.
 #
 #   CSV("CSV,data").read

BTW, is CSV::TSV intentional? (::TSV may be easy to use but it may break other library's API that defines ::TSV...)

jsxs0 commented 4 weeks ago

@kou Thanks for the feedback! Updated and pushed a new commit.

kou commented 2 weeks ago

Could you add some tests for this change? Could you update the PR description for the latest changes?

jsxs0 commented 2 weeks ago

Could you add some tests for this change? Could you update the PR description for the latest changes?

@kou Thanks, added tests and updated the description.

kou commented 2 weeks ago

Did you run added tests on your local machine? Or did you enable GitHub Actions on your fork?

We need to add CSV:: to all TSV references.

jsxs0 commented 3 days ago

Did you run added tests on your local machine? Or did you enable GitHub Actions on your fork?

We need to add CSV:: to all TSV references.

Thanks. The fix was a straightforward namespace correction that aligns the test code with the actual implementation in lib/csv.rb.

kou commented 3 days ago

Thanks.