Skip to content

Commit 102d95c

Browse files
authored
Merge pull request #2002 from tk0miya/csv_read
stdlib: Update signature of CSV.read
2 parents ff96e83 + 7e86e10 commit 102d95c

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
@@ -47,4 +47,32 @@ def test_foreach
4747
assert_send_type "(String path, headers: bool, **untyped) -> Enumerator[CSV::Row, void]",
4848
CSV, :foreach, path, headers: true, encoding: 'UTF-8'
4949
end
50+
51+
def test_read
52+
tmpdir = Dir.mktmpdir
53+
path = File.join(tmpdir, "example.csv")
54+
File.write(path, "a,b,c\n1,2,3\n")
55+
56+
assert_send_type "(String path, headers: true) -> CSV::Table[CSV::Row]",
57+
CSV, :read, path, headers: true
58+
assert_send_type "(IO path, headers: true) -> CSV::Table[CSV::Row]",
59+
CSV, :read, File.open(path), headers: true
60+
assert_send_type "(String path, headers: :first_row) -> CSV::Table[CSV::Row]",
61+
CSV, :read, path, headers: :first_row
62+
assert_send_type "(IO path, headers: :first_row) -> CSV::Table[CSV::Row]",
63+
CSV, :read, File.open(path), headers: :first_row
64+
assert_send_type "(String path, headers: Array[String]) -> CSV::Table[CSV::Row]",
65+
CSV, :read, path, headers: %w[foo bar baz]
66+
assert_send_type "(IO path, headers: Array[String]) -> CSV::Table[CSV::Row]",
67+
CSV, :read, File.open(path), headers: %w[foo bar baz]
68+
assert_send_type "(String path, headers: String) -> CSV::Table[CSV::Row]",
69+
CSV, :read, path, headers: "foo,bar,baz"
70+
assert_send_type "(IO path, headers: String) -> CSV::Table[CSV::Row]",
71+
CSV, :read, File.open(path), headers: "foo,bar,baz"
72+
73+
assert_send_type "(String path) -> Array[Array[String?]]",
74+
CSV, :read, path
75+
assert_send_type "(IO path) -> Array[Array[String?]]",
76+
CSV, :read, File.open(path)
77+
end
5078
end

0 commit comments

Comments
 (0)