337
337
#
338
338
# ==== Options for Parsing
339
339
#
340
- # :include: ../doc/options/common/col_sep.rdoc
340
+ # Options for parsing, described in detail below, include:
341
+ # - +row_sep+: Specifies the row separator; used to delimit rows.
342
+ # - +col_sep+: Specifies the column separator; used to delimit fields.
343
+ # - +quote_char+: Specifies the quote character; used to quote fields.
344
+ # - +field_size_limit+: Specifies the maximum field size allowed.
345
+ # - +converters+: Specifies the field converters to be used.
346
+ # - +unconverted_fields+: Specifies whether unconverted fields are to be available.
347
+ # - +headers+: Specifies whether data contains headers,
348
+ # or specifies the headers themselves.
349
+ # - +return_headers+: Specifies whether headers are to be returned.
350
+ # - +header_converters+: Specifies the header converters to be used.
351
+ # - +skip_blanks+: Specifies whether blanks lines are to be ignored.
352
+ # - +skip_lines+: Specifies how comments lines are to be recognized.
353
+ # - +strip+: Specifies whether leading and trailing whitespace are
354
+ # to be stripped from fields..
355
+ # - +liberal_parsing+: Specifies whether \CSV should attempt to parse
356
+ # non-compliant data.
357
+ # - +nil_value+: Specifies the object that is to be substituted for each null (no-text) field.
358
+ # - +empty_value+: Specifies the object that is to be substituted for each empty field.
341
359
#
342
360
# :include: ../doc/options/common/row_sep.rdoc
343
361
#
362
+ # :include: ../doc/options/common/col_sep.rdoc
363
+ #
344
364
# :include: ../doc/options/common/quote_char.rdoc
345
365
#
346
366
# :include: ../doc/options/parsing/field_size_limit.rdoc
369
389
#
370
390
# ==== Options for Generating
371
391
#
372
- # :include: ../doc/options/common/col_sep.rdoc
392
+ # Options for generating, described in detail below, include:
393
+ # - +row_sep+: Specifies the row separator; used to delimit rows.
394
+ # - +col_sep+: Specifies the column separator; used to delimit fields.
395
+ # - +quote_char+: Specifies the quote character; used to quote fields.
396
+ # - +write_headers+: Specifies whether headers are to be written.
397
+ # - +force_quotes+: Specifies whether each output field is to be quoted.
398
+ # - +quote_empty+: Specifies whether each empty output field is to be quoted.
399
+ # - +write_converters+: Specifies the field converters to be used in writing.
400
+ # - +write_nil_value+: Specifies the object that is to be substituted for each +nil+-valued field.
401
+ # - +write_empty_value+: Specifies the object that is to be substituted for each empty field.
373
402
#
374
403
# :include: ../doc/options/common/row_sep.rdoc
375
404
#
405
+ # :include: ../doc/options/common/col_sep.rdoc
406
+ #
376
407
# :include: ../doc/options/common/quote_char.rdoc
377
408
#
378
409
# :include: ../doc/options/generating/write_headers.rdoc
@@ -1211,11 +1242,11 @@ def generate_line(row, **options)
1211
1242
#
1212
1243
# Create a \CSV object using a file path:
1213
1244
# csv = CSV.open(path)
1214
- # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1245
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1215
1246
#
1216
1247
# Create a \CSV object using an open \File:
1217
1248
# csv = CSV.open(File.open(path))
1218
- # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1249
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1219
1250
#
1220
1251
# ---
1221
1252
#
@@ -1224,15 +1255,15 @@ def generate_line(row, **options)
1224
1255
#
1225
1256
# Using a file path:
1226
1257
# csv = CSV.open(path) {|csv| p csv}
1227
- # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1258
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1228
1259
# Output:
1229
- # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1260
+ # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1230
1261
#
1231
1262
# Using an open \File:
1232
1263
# csv = CSV.open(File.open(path)) {|csv| p csv}
1233
- # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1264
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1234
1265
# Output:
1235
- # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\ n" quote_char:"\"">
1266
+ # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
1236
1267
#
1237
1268
# ---
1238
1269
#
@@ -1642,71 +1673,62 @@ def initialize(data,
1642
1673
writer if @writer_options [ :write_headers ]
1643
1674
end
1644
1675
1645
- #
1646
- # The encoded <tt>:col_sep</tt> used in parsing and writing.
1647
- # See CSV::new for details.
1648
- #
1676
+ # Returns the encoded column separator; used for parsing and writing;
1677
+ # see {Option +col_sep+}[#class-CSV-label-Option+col_sep]:
1678
+ # CSV.new('').col_sep # => ","
1649
1679
def col_sep
1650
1680
parser . column_separator
1651
1681
end
1652
1682
1653
- #
1654
- # The encoded <tt>:row_sep</tt> used in parsing and writing.
1655
- # See CSV::new for details.
1656
- #
1683
+ # Returns the encoded row separator; used for parsing and writing;
1684
+ # see {Option +row_sep+}[#class-CSV-label-Option+row_sep]:
1685
+ # CSV.new('').row_sep # => "\n"
1657
1686
def row_sep
1658
1687
parser . row_separator
1659
1688
end
1660
1689
1661
- #
1662
- # The encoded <tt>:quote_char</tt> used in parsing and writing.
1663
- # See CSV::new for details.
1664
- #
1690
+ # Returns the encoded quote character; used for parsing and writing;
1691
+ # see {Option +quote_char+}[#class-CSV-label-Option+quote_char]:
1692
+ # CSV.new('').quote_char # => "\""
1665
1693
def quote_char
1666
1694
parser . quote_character
1667
1695
end
1668
1696
1669
- #
1670
- # The limit for field size, if any.
1671
- # See CSV::new for details.
1672
- #
1697
+ # Returns the limit for field size; used for parsing;
1698
+ # see {Option +field_size_limit+}[#class-CSV-label-Option+field_size_limit]:
1699
+ # CSV.new('').field_size_limit # => nil
1673
1700
def field_size_limit
1674
1701
parser . field_size_limit
1675
1702
end
1676
1703
1677
- #
1678
- # The regex marking a line as a comment.
1679
- # See CSV::new for details.
1680
- #
1704
+ # Returns the \Regexp used to identify comment lines; used for parsing;
1705
+ # see {Option +skip_lines+}[#class-CSV-label-Option+skip_lines]:
1706
+ # CSV.new('').skip_lines # => nil
1681
1707
def skip_lines
1682
1708
parser . skip_lines
1683
1709
end
1684
1710
1685
- #
1686
- # Returns the current list of converters in effect. See CSV::new for details.
1687
- # Built-in converters will be returned by name, while others will be returned
1688
- # as is.
1689
- #
1711
+ # Returns an \Array containing field converters; used for parsing;
1712
+ # see {Option +converters+}[#class-CSV-label-Option+converters]:
1713
+ # CSV.new('').converters # => []
1690
1714
def converters
1691
1715
parser_fields_converter . map do |converter |
1692
1716
name = Converters . rassoc ( converter )
1693
1717
name ? name . first : converter
1694
1718
end
1695
1719
end
1696
1720
1697
- #
1698
- # Returns +true+ if unconverted_fields() to parsed results.
1699
- # See CSV::new for details.
1700
- #
1721
+ # Returns the value that determines whether unconverted fields are to be
1722
+ # available; used for parsing;
1723
+ # see {Option +unconverted_fields+}[#class- CSV-label-Option+unconverted_fields]:
1724
+ # CSV.new('').unconverted_fields? # => nil
1701
1725
def unconverted_fields?
1702
1726
parser . unconverted_fields?
1703
1727
end
1704
1728
1705
- #
1706
- # Returns +nil+ if headers will not be used, +true+ if they will but have not
1707
- # yet been read, or the actual headers after they have been read.
1708
- # See CSV::new for details.
1709
- #
1729
+ # Returns the value that determines whether headers are used; used for parsing;
1730
+ # see {Option +headers+}[#class-CSV-label-Option+headers]:
1731
+ # CSV.new('').headers # => nil
1710
1732
def headers
1711
1733
if @writer
1712
1734
@writer . headers
@@ -1718,62 +1740,84 @@ def headers
1718
1740
raw_headers
1719
1741
end
1720
1742
end
1721
- #
1722
- # Returns +true+ if headers will be returned as a row of results.
1723
- # See CSV::new for details.
1724
- #
1743
+
1744
+ # Returns the value that determines whether headers are to be returned; used for parsing;
1745
+ # see {Option +return_headers+}[#class- CSV-label-Option+return_headers]:
1746
+ # CSV.new('').return_headers? # => false
1725
1747
def return_headers?
1726
1748
parser . return_headers?
1727
1749
end
1728
1750
1729
- #
1730
- # Returns +true+ if headers are written in output.
1731
- # See CSV::new for details.
1732
- #
1751
+ # Returns the value that determines whether headers are to be written; used for generating;
1752
+ # see {Option +write_headers+}[#class-CSV-label-Option+write_headers]:
1753
+ # CSV.new('').write_headers? # => nil
1733
1754
def write_headers?
1734
1755
@writer_options [ :write_headers ]
1735
1756
end
1736
1757
1737
- #
1738
- # Returns the current list of converters in effect for headers. See CSV::new
1739
- # for details. Built-in converters will be returned by name, while others
1740
- # will be returned as is.
1741
- #
1758
+ # Returns an \Array containing header converters; used for parsing;
1759
+ # see {Option +header_converters+}[#class-CSV-label-Option+header_converters]:
1760
+ # CSV.new('').header_converters # => []
1742
1761
def header_converters
1743
1762
header_fields_converter . map do |converter |
1744
1763
name = HeaderConverters . rassoc ( converter )
1745
1764
name ? name . first : converter
1746
1765
end
1747
1766
end
1748
1767
1749
- #
1750
- # Returns +true+ blank lines are skipped by the parser. See CSV::new
1751
- # for details.
1752
- #
1768
+ # Returns the value that determines whether blank lines are to be ignored; used for parsing;
1769
+ # see {Option +skip_blanks+}[#class-CSV-label-Option+skip_blanks]:
1770
+ # CSV.new('').skip_blanks? # => false
1753
1771
def skip_blanks?
1754
1772
parser . skip_blanks?
1755
1773
end
1756
1774
1757
- # Returns +true+ if all output fields are quoted. See CSV::new for details.
1775
+ # Returns the value that determines whether all output fields are to be quoted;
1776
+ # used for generating;
1777
+ # see {Option +force_quotes+}[#class-CSV-label-Option+force_quotes]:
1778
+ # CSV.new('').force_quotes? # => false
1758
1779
def force_quotes?
1759
1780
@writer_options [ :force_quotes ]
1760
1781
end
1761
1782
1762
- # Returns +true+ if illegal input is handled. See CSV::new for details.
1783
+ # Returns the value that determines whether illegal input is to be handled; used for parsing;
1784
+ # see {Option +liberal_parsing+}[#class-CSV-label-Option+liberal_parsing]:
1785
+ # CSV.new('').liberal_parsing? # => false
1763
1786
def liberal_parsing?
1764
1787
parser . liberal_parsing?
1765
1788
end
1766
1789
1767
- #
1768
- # The Encoding CSV is parsing or writing in. This will be the Encoding you
1769
- # receive parsed data in and/or the Encoding data will be written in.
1770
- #
1790
+ # Returns the encoding used for parsing and generating;
1791
+ # see {CSV and Character Encodings (M17n or Multilingualization)}[#class-CSV-label-CSV+and+Character+Encodings+-28M17n+or+Multilingualization-29]:
1792
+ # CSV.new('').encoding # => #<Encoding:UTF-8>
1771
1793
attr_reader :encoding
1772
1794
1795
+ # Returns the count of the rows parsed or generated.
1773
1796
#
1774
- # The line number of the last row read from this file. Fields with nested
1775
- # line-end characters will not affect this count.
1776
- #
1797
+ # Parsing:
1798
+ # string = "foo,0\nbar,1\nbaz,2\n"
1799
+ # path = 't.csv'
1800
+ # File.write(path, string)
1801
+ # CSV.open(path) do |csv|
1802
+ # csv.each do |row|
1803
+ # p [csv.lineno, row]
1804
+ # end
1805
+ # end
1806
+ # Output:
1807
+ # [1, ["foo", "0"]]
1808
+ # [2, ["bar", "1"]]
1809
+ # [3, ["baz", "2"]]
1810
+ #
1811
+ # Generating:
1812
+ # CSV.generate do |csv|
1813
+ # p csv.lineno; csv << ['foo', 0]
1814
+ # p csv.lineno; csv << ['bar', 1]
1815
+ # p csv.lineno; csv << ['baz', 2]
1816
+ # end
1817
+ # Output:
1818
+ # 0
1819
+ # 1
1820
+ # 2
1777
1821
def lineno
1778
1822
if @writer
1779
1823
@writer . lineno
@@ -1782,9 +1826,19 @@ def lineno
1782
1826
end
1783
1827
end
1784
1828
1785
- #
1786
- # The last row read from this file.
1787
- #
1829
+ # Returns the line most recently read:
1830
+ # string = "foo,0\nbar,1\nbaz,2\n"
1831
+ # path = 't.csv'
1832
+ # File.write(path, string)
1833
+ # CSV.open(path) do |csv|
1834
+ # csv.each do |row|
1835
+ # p [csv.lineno, csv.line]
1836
+ # end
1837
+ # end
1838
+ # Output:
1839
+ # [1, "foo,0\n"]
1840
+ # [2, "bar,1\n"]
1841
+ # [3, "baz,2\n"]
1788
1842
def line
1789
1843
parser . line
1790
1844
end
0 commit comments