8000 [ruby/csv] RDoc for parse_line (adds headers examples) (#143) · nobu/ruby@93dae59 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93dae59

Browse files
BurdetteLamarnobu
authored andcommitted
[ruby/csv] RDoc for parse_line (adds headers examples) (ruby#143)
* RDoc for parse_line (adds headers examples) * RDoc for parse_line (adds headers examples) ruby/csv@a161be928e
1 parent 8f3231d commit 93dae59

File tree

1 file changed

+40
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1288,35 +1288,59 @@ def parse(str, **options, &block)
12881288
end
12891289

12901290
# :call-seq:
1291-
# CSV.parse_line(string)
1292-
# CSV.parse_line(io)
1293-
# CSV.parse_line(string, **options)
1294-
# CSV.parse_line(io, **options)
1295-
#
1296-
# Returns the new \Array created by parsing the first line of +string+ or +io+
1291+
# CSV.parse_line(string) -> new_array or nil
1292+
# CSV.parse_line(io) -> new_array or nil
1293+
# CSV.parse_line(string, **options) -> new_array or nil
1294+
# CSV.parse_line(io, **options) -> new_array or nil
1295+
# CSV.parse_line(string, headers: true, **options) -> csv_row or nil
1296+
# CSV.parse_line(io, headers: true, **options) -> csv_row or nil
1297+
#
1298+
# Returns the data created by parsing the first line of +string+ or +io+
12971299
# using the specified +options+.
12981300
#
12991301
# - Argument +string+ should be a \String object;
13001302
# it will be put into a new StringIO object positioned at the beginning.
13011303
# :include: ../doc/argument_io.rdoc
1302-
# To position at the end, for appending, use method CSV.generate.
1303-
# For any other positioning, pass a preset \StringIO object instead.
13041304
# - Argument +options+: see {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
13051305
#
1306-
# ---
1307-
# Returns data from the first line from a String object:
1308-
# CSV.parse_line('foo,0') # => ["foo", "0"]
1306+
# ====== Without Option +headers+
13091307
#
1310-
# Returns data from the first line from a File object:
1311-
# File.write('t.csv', 'foo,0')
1312-
# CSV.parse_line(File.open('t.csv')) # => ["foo", "0"]
1308+
# Without option +headers+, returns the first row as a new \Array.
13131309
#
1314-
# Ignores lines after the first:
1315-
# CSV.parse_line("foo,0\nbar,1\nbaz,2") # => ["foo", "0"]
1310+
# These examples assume prior execution of:
1311+
# string = "foo,0\nbar,1\nbaz,2\n"
1312+
# path = 't.csv'
1313+
# File.write(path, string)
1314+
#
1315+
# Parse the first line from a \String object:
1316+
# CSV.parse_line(string) # => ["foo", "0"]
1317+
#
1318+
# Parse the first line from a File object:
1319+
# File.open(path) do |file|
1320+
# CSV.parse_line(file) # => ["foo", "0"]
1321+
# end # => ["foo", "0"]
13161322
#
13171323
# Returns +nil+ if the argument is an empty \String:
13181324
# CSV.parse_line('') # => nil
13191325
#
1326+
# ====== With Option +headers+
1327+
#
1328+
# With {option +headers+}[#class-CSV-label-Option+headers],
1329+
# returns the first row as a CSV::Row object.
1330+
#
1331+
# These examples assume prior execution of:
1332+
# string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
1333+
# path = 't.csv'
1334+
# File.write(path, string)
1335+
#
1336+
# Parse the first line from a \String object:
1337+
# CSV.parse_line(string, headers: true) # => #<CSV::Row "Name":"foo" "Count":"0">
1338+
#
1339+
# Parse the first line from a File object:
1340+
# File.open(path) do |file|
1341+
# CSV.parse_line(file, headers: true)
1342+
# end # => #<CSV::Row "Name":"foo" "Count":"0">
1343+
#
13201344
# ---
13211345
#
13221346
# Raises an exception if the argument is +nil+: