@@ -1352,36 +1352,52 @@ def parse_line(line, **options)
1352
1352
end
1353
1353
1354
1354
#
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.
1367
1362
#
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>
1368
1374
def read ( path , **options )
1369
1375
open ( path , **options ) { |csv | csv . read }
1370
1376
end
1371
1377
1372
- # Alias for CSV::read().
1378
+ # :call-seq:
1379
+ # CSV.readlines(source, **options)
1380
+ #
1381
+ # Alias for CSV.read.
1373
1382
def readlines ( path , **options )
1374
1383
read ( path , **options )
1375
1384
end
1376
1385
1386
+ # :call-seq:
1387
+ # CSV.table(source, **options)
1377
1388
#
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+
1379
1393
#
1380
- # CSV.read( path, { headers: true,
1381
- # converters: :numeric,
1382
- # header_converters: :symbol }.merge(options) )
1394
+ # Returns a CSV::Table object.
1383
1395
#
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>
1385
1401
def table ( path , **options )
1386
1402
default_options = {
1387
1403
headers : true ,
@@ -1793,11 +1809,28 @@ def each(&block)
1793
1809
parser_enumerator . each ( &block )
1794
1810
end
1795
1811
1812
+ # :call-seq:
1813
+ # read
1796
1814
#
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.
1798
1818
#
1799
1819
# The data source must be open for reading.
1800
1820
#
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>
1801
1834
def read
1802
1835
rows = to_a
1803
1836
if parser . use_headers?
0 commit comments