@@ -1133,6 +1133,8 @@ def open(filename, mode="r", **options)
1133
1133
# :call-seq:
1134
1134
# parse(string) -> array_of_arrays
1135
1135
# parse(io) -> array_of_arrays
1136
+ # parse(string, headers: ..., **options) -> csv_table
1137
+ # parse(io, headers: ..., **options) -> csv_table
1136
1138
# parse(string, **options) {|row| ... } -> integer
1137
1139
# parse(io, **options) {|row| ... } -> integer
1138
1140
#
@@ -1148,6 +1150,10 @@ def open(filename, mode="r", **options)
1148
1150
# path = 't.csv'
1149
1151
# File.write(path, string)
1150
1152
#
1153
+ # ====== Without Option +headers+
1154
+ #
1155
+ # Without option +headers+, returns an \Array of Arrays or an integer.
1156
+ #
1151
1157
# ---
1152
1158
#
1153
1159
# With no block given, returns an \Array of Arrays formed from the source.
@@ -1157,7 +1163,9 @@ def open(filename, mode="r", **options)
1157
1163
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1158
1164
#
1159
1165
# 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
1161
1169
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1162
1170
#
1163
1171
# ---
@@ -1173,13 +1181,57 @@ def open(filename, mode="r", **options)
1173
1181
# ["baz", "2"]
1174
1182
#
1175
1183
# 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
1177
1187
#
1178
1188
# Output:
1179
1189
# ["foo", "0"]
1180
1190
# ["bar", "1"]
1181
1191
# ["baz", "2"]
1182
1192
#
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
+ #
1183
1235
# ---
1184
1236
#
1185
1237
# Raises an exception if the argument is not a \String object or \IO object:
0 commit comments