Skip to content

Commit 848c760

Browse files
Add headers cases to CSV.parse (#141)
* Add headers cases to CSV.parse * Adjust call-seq for CSV.parse * Update csv.rb
1 parent eb838de commit 848c760

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

lib/csv.rb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,8 @@ def open(filename, mode="r", **options)
11331133
# :call-seq:
11341134
# parse(string) -> array_of_arrays
11351135
# parse(io) -> array_of_arrays
1136+
# parse(string, headers: ..., **options) -> csv_table
1137+
# parse(io, headers: ..., **options) -> csv_table
11361138
# parse(string, **options) {|row| ... } -> integer
11371139
# parse(io, **options) {|row| ... } -> integer
11381140
#
@@ -1148,6 +1150,10 @@ def open(filename, mode="r", **options)
11481150
# path = 't.csv'
11491151
# File.write(path, string)
11501152
#
1153+
# ====== Without Option +headers+
1154+
#
1155+
# Without option +headers+, returns an \Array of Arrays or an integer.
1156+
#
11511157
# ---
11521158
#
11531159
# With no block given, returns an \Array of Arrays formed from the source.
@@ -1157,7 +1163,9 @@ def open(filename, mode="r", **options)
11571163
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
11581164
#
11591165
# Parse an open \File:
1160-
# a_of_a = CSV.parse(File.open(path))
1166+
# a_of_a = File.open(path) do |file|
1167+
# CSV.parse(file)
1168+
# end
11611169
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
11621170
#
11631171
# ---
@@ -1173,13 +1181,57 @@ def open(filename, mode="r", **options)
11731181
# ["baz", "2"]
11741182
#
11751183
# Parse an open \File:
1176-
# CSV.parse(File.open(path)) {|row| p row } # => 18
1184+
# File.open(path) do |file|
1185+
# CSV.parse(file) {|row| p row } # => 18
1186+
# end
11771187
#
11781188
# Output:
11791189
# ["foo", "0"]
11801190
# ["bar", "1"]
11811191
# ["baz", "2"]
11821192
#
1193+
# ====== With Option +headers+
1194+
#
1195+
# With {option +headers+}[#class-CSV-label-Option+headers],
1196+
# returns a new CSV::Table object or an integer.
1197+
#
1198+
# ---
1199+
#
1200+
# With no block given, returns a CSV::Table object formed from the source.
1201+
#
1202+
# Parse a \String:
1203+
# csv_table = CSV.parse(string, headers: ['Name', 'Count'])
1204+
# csv_table # => #<CSV::Table mode:col_or_row row_count:5>
1205+
#
1206+
# Parse an open \File:
1207+
# csv_table = File.open(path) do |file|
1208+
# CSV.parse(file, headers: ['Name', 'Count'])
1209+
# end
1210+
# csv_table # => #<CSV::Table mode:col_or_row row_count:4>
1211+
#
1212+
# ---
1213+
#
1214+
# With a block given, calls the block with each parsed row,
1215+
# which has been formed into a CSV::Row object:
1216+
#
1217+
# Parse a \String:
1218+
# CSV.parse(string, headers: ['Name', 'Count']) {|row| p row } # => 18
1219+
#
1220+
# Output:
1221+
# # <CSV::Row "Name":"foo" "Count":"0">
1222+
# # <CSV::Row "Name":"bar" "Count":"1">
1223+
# # <CSV::Row "Name":"baz" "Count":"2">
1224+
#
1225+
# Parse an open \File:
1226+
# File.open(path) do |file|
1227+
# CSV.parse(file, headers: ['Name', 'Count']) {|row| p row } # => 18
1228+
# end
1229+
#
1230+
# Output:
1231+
# # <CSV::Row "Name":"foo" "Count":"0">
1232+
# # <CSV::Row "Name":"bar" "Count":"1">
1233+
# # <CSV::Row "Name":"baz" "Count":"2">
1234+
#
11831235
# ---
11841236
#
11851237
# Raises an exception if the argument is not a \String object or \IO object:

0 commit comments

Comments
 (0)