-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSV::TSV class for tab-separated values (#319)
GitHub: fix GH-272 This adds `CSV::TSV` that uses `\t` as the default column separator. How to use: ```ruby require "csv" # Read TSV file with default tab separator CSV::TSV.read("data.tsv") # Parse TSV string CSV::TSV.parse("a\tb\tc") # Generate TSV content CSV::TSV.generate do |tsv| tsv << ["a", "b", "c"] tsv << [1, 2, 3] end ``` Reported by kojix2. Thanks!!! --------- Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require_relative "helper" | ||
|
||
class TestTSV < Test::Unit::TestCase | ||
def test_default_separator | ||
tsv = CSV::TSV.new(String.new) | ||
assert_equal("\t", tsv.col_sep) | ||
end | ||
|
||
def test_override_separator | ||
tsv = CSV::TSV.new(String.new, col_sep: ",") | ||
assert_equal(",", tsv.col_sep) | ||
end | ||
|
||
def test_read_tsv_data | ||
data = "a\tb\tc\n1\t2\t3" | ||
result = CSV::TSV.parse(data) | ||
assert_equal([["a", "b", "c"], ["1", "2", "3"]], result.to_a) | ||
end | ||
|
||
def test_write_tsv_data | ||
output = String.new | ||
CSV::TSV.generate(output) do |tsv| | ||
tsv << ["a", "b", "c"] | ||
tsv << ["1", "2", "3"] | ||
end | ||
assert_equal("a\tb\tc\n1\t2\t3\n", output) | ||
end | ||
|
||
def test_inheritance | ||
assert_kind_of(CSV, CSV::TSV.new(String.new)) | ||
end | ||
end |