@@ -1352,36 +1352,52 @@ def parse_line(line, **options)
13521352 end
13531353
13541354 #
1355- # Use to slurp a CSV file into an Array of Arrays. Pass the +path+ to the
1356- # file and +options+.
1357- # See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1358- #
1359- # This method also understands
1360- # an additional <tt>:encoding</tt> parameter that you can use to specify the
1361- # Encoding of the data in the file to be read. You must provide this unless
1362- # your data is in Encoding::default_external(). CSV will use this to determine
1363- # how to parse the data. You may provide a second Encoding to have the data
1364- # transcoded as it is read. For example,
1365- # <tt>encoding: "UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file
1366- # but transcode it to UTF-8 before CSV parses it.
1355+ # :call-seq:
1356+ # read(source, **options) -> array_of_arrays
1357+ # read(source, headers: true, **options) -> csv_table
1358+ #
1359+ # Opens the given +source+ with the given +options+ (see CSV.open),
1360+ # reads the source (see CSV#read), and returns the result,
1361+ # which will be either an \Array of Arrays or a CSV::Table.
13671362 #
1363+ # Without headers:
1364+ # string = "foo,0\nbar,1\nbaz,2\n"
1365+ # path = 't.csv'
1366+ # File.write(path, string)
1367+ # CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1368+ #
1369+ # With headers:
1370+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1371+ # path = 't.csv'
1372+ # File.write(path, string)
1373+ # CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
13681374 def read ( path , **options )
13691375 open ( path , **options ) { |csv | csv . read }
13701376 end
13711377
1372- # Alias for CSV::read().
1378+ # :call-seq:
1379+ # CSV.readlines(source, **options)
1380+ #
1381+ # Alias for CSV.read.
13731382 def readlines ( path , **options )
13741383 read ( path , **options )
13751384 end
13761385
1386+ # :call-seq:
1387+ # CSV.table(source, **options)
13771388 #
1378- # A shortcut for:
1389+ # Calls CSV.read with +source+, +options+, and certain default options:
1390+ # - +headers+: +true+
1391+ # - +converbers+: +:numeric+
1392+ # - +header_converters+: +:symbol+
13791393 #
1380- # CSV.read( path, { headers: true,
1381- # converters: :numeric,
1382- # header_converters: :symbol }.merge(options) )
1394+ # Returns a CSV::Table object.
13831395 #
1384- # See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1396+ # Example:
1397+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1398+ # path = 't.csv'
1399+ # File.write(path, string)
1400+ # CSV.table(path # => #<CSV::Table mode:col_or_row row_count:4>
13851401 def table ( path , **options )
13861402 default_options = {
13871403 headers : true ,
@@ -1793,11 +1809,28 @@ def each(&block)
17931809 parser_enumerator . each ( &block )
17941810 end
17951811
1812+ # :call-seq:
1813+ # read
17961814 #
1797- # Slurps the remaining rows and returns an Array of Arrays.
1815+ # Forms the remaining rows from +self+ into:
1816+ # - A CSV::Table object, if headers are in use.
1817+ # - An Array of Arrays, otherwise.
17981818 #
17991819 # The data source must be open for reading.
18001820 #
1821+ # Without headers:
1822+ # string = "foo,0\nbar,1\nbaz,2\n"
1823+ # path = 't.csv'
1824+ # File.write(path, string)
1825+ # csv = CSV.open(path)
1826+ # csv.read # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1827+ #
1828+ # With headers:
1829+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1830+ # path = 't.csv'
1831+ # File.write(path, string)
1832+ # csv = CSV.open(path, headers: true)
1833+ # csv.read # => #<CSV::Table mode:col_or_row row_count:4>
18011834 def read
18021835 rows = to_a
18031836 if parser . use_headers?
0 commit comments