Skip to content

Commit 7e86e10

Browse files
committed
stdlib: Update signature of CSV.read
* It takes an IO object as `path` argument * It returns CSV::Table object when headers option given refs: * https://docs.ruby-lang.org/en/3.3/CSV.html#method-c-read * https://docs.ruby-lang.org/en/3.3/CSV.html#class-CSV-label-Option+headers
1 parent c3e8299 commit 7e86e10

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

stdlib/csv/0/csv.rbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,8 @@ class CSV < Object
20312031
# File.write(path, string)
20322032
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
20332033
#
2034-
def self.read: (String path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]
2034+
def self.read: (String | IO path, headers: true | :first_row | Array[untyped] | String, **untyped options) -> ::CSV::Table[CSV::Row]
2035+
| (String | IO path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]
20352036

20362037
# <!--
20372038
# rdoc-file=lib/csv.rb

test/stdlib/CSV_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,32 @@ def test_foreach
3535
assert_send_type "(String path, headers: bool, **untyped) -> Enumerator[CSV::Row, void]",
3636
CSV, :foreach, path, headers: true, encoding: 'UTF-8'
3737
end
38+
39+
def test_read
40+
tmpdir = Dir.mktmpdir
41+
path = File.join(tmpdir, "example.csv")
42+
File.write(path, "a,b,c\n1,2,3\n")
43+
44+
assert_send_type "(String path, headers: true) -> CSV::Table[CSV::Row]",
45+
CSV, :read, path, headers: true
46+
assert_send_type "(IO path, headers: true) -> CSV::Table[CSV::Row]",
47+
CSV, :read, File.open(path), headers: true
48+
assert_send_type "(String path, headers: :first_row) -> CSV::Table[CSV::Row]",
49+
CSV, :read, path, headers: :first_row
50+
assert_send_type "(IO path, headers: :first_row) -> CSV::Table[CSV::Row]",
51+
CSV, :read, File.open(path), headers: :first_row
52+
assert_send_type "(String path, headers: Array[String]) -> CSV::Table[CSV::Row]",
53+
CSV, :read, path, headers: %w[foo bar baz]
54+
assert_send_type "(IO path, headers: Array[String]) -> CSV::Table[CSV::Row]",
55+
CSV, :read, File.open(path), headers: %w[foo bar baz]
56+
assert_send_type "(String path, headers: String) -> CSV::Table[CSV::Row]",
57+
CSV, :read, path, headers: "foo,bar,baz"
58+
assert_send_type "(IO path, headers: String) -> CSV::Table[CSV::Row]",
59+
CSV, :read, File.open(path), headers: "foo,bar,baz"
60+
61+
assert_send_type "(String path) -> Array[Array[String?]]",
62+
CSV, :read, path
63+
assert_send_type "(IO path) -> Array[Array[String?]]",
64+
CSV, :read, File.open(path)
65+
end
3866
end

0 commit comments

Comments
 (0)