Skip to content

Commit 55cede4

Browse files
authored
Merge pull request #2099 from tk0miya/csv/Array.to_csv
stdlib: csv: Add types for Array#to_csv and String#parse_csv
2 parents da70891 + 153451c commit 55cede4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

stdlib/csv/0/csv.rbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,3 +3756,21 @@ class CSV::Table[out Elem] < Object
37563756
#
37573757
def values_at: (*untyped indices_or_headers) -> untyped
37583758
end
3759+
3760+
%a{annotate:rdoc:skip}
3761+
class Array[unchecked out Elem] < Object
3762+
# Equivalent to CSV::generate_line(self, options)
3763+
#
3764+
# ["CSV", "data"].to_csv
3765+
# #=> "CSV,data\n"
3766+
def to_csv: (**untyped options) -> String
3767+
end
3768+
3769+
%a{annotate:rdoc:skip}
3770+
class String
3771+
# Equivalent to CSV::parse_line(self, options)
3772+
#
3773+
# "CSV,data".parse_csv
3774+
# #=> ["CSV", "data"]
3775+
def parse_csv: (**untyped options) -> ::Array[String?]?
3776+
end

test/stdlib/CSV_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,31 @@ def test_headers
9696
csv, :headers
9797
end
9898
end
99+
100+
class CSVArrayTest < Test::Unit::TestCase
101+
include TestHelper
102+
103+
library "csv"
104+
testing "Array[untyped]"
105+
106+
def test_to_csv_with_array
107+
assert_send_type "() -> String",
108+
[1, 2, 3], :to_csv
109+
assert_send_type "(**untyped) -> String",
110+
[1, 2, 3], :to_csv, col_sep: '\t'
111+
end
112+
end
113+
114+
class CSVStringTest < Test::Unit::TestCase
115+
include TestHelper
116+
117+
library "csv"
118+
testing "String"
119+
120+
def test_parse_csv_with_string
121+
assert_send_type "() -> Array[String?]",
122+
"1,2,3", :parse_csv
123+
assert_send_type "(**untyped) -> Array[String?]",
124+
"1,2,3", :parse_csv, col_sep: '\t'
125+
end
126+
end

0 commit comments

Comments
 (0)