337337#
338338# ==== Options for Parsing
339339#
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.
341359#
342360# :include: ../doc/options/common/row_sep.rdoc
343361#
362+ # :include: ../doc/options/common/col_sep.rdoc
363+ #
344364# :include: ../doc/options/common/quote_char.rdoc
345365#
346366# :include: ../doc/options/parsing/field_size_limit.rdoc
369389#
370390# ==== Options for Generating
371391#
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.
373402#
374403# :include: ../doc/options/common/row_sep.rdoc
375404#
405+ # :include: ../doc/options/common/col_sep.rdoc
406+ #
376407# :include: ../doc/options/common/quote_char.rdoc
377408#
378409# :include: ../doc/options/generating/write_headers.rdoc
@@ -1211,11 +1242,11 @@ def generate_line(row, **options)
12111242 #
12121243 # Create a \CSV object using a file path:
12131244 # 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:"\"">
12151246 #
12161247 # Create a \CSV object using an open \File:
12171248 # 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:"\"">
12191250 #
12201251 # ---
12211252 #
@@ -1224,15 +1255,15 @@ def generate_line(row, **options)
12241255 #
12251256 # Using a file path:
12261257 # 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:"\"">
12281259 # 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:"\"">
12301261 #
12311262 # Using an open \File:
12321263 # 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:"\"">
12341265 # 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:"\"">
12361267 #
12371268 # ---
12381269 #
@@ -1642,71 +1673,62 @@ def initialize(data,
16421673 writer if @writer_options [ :write_headers ]
16431674 end
16441675
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 # => ","
16491679 def col_sep
16501680 parser . column_separator
16511681 end
16521682
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"
16571686 def row_sep
16581687 parser . row_separator
16591688 end
16601689
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 # => "\""
16651693 def quote_char
16661694 parser . quote_character
16671695 end
16681696
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
16731700 def field_size_limit
16741701 parser . field_size_limit
16751702 end
16761703
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
16811707 def skip_lines
16821708 parser . skip_lines
16831709 end
16841710
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 # => []
16901714 def converters
16911715 parser_fields_converter . map do |converter |
16921716 name = Converters . rassoc ( converter )
16931717 name ? name . first : converter
16941718 end
16951719 end
16961720
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
17011725 def unconverted_fields?
17021726 parser . unconverted_fields?
17031727 end
17041728
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
17101732 def headers
17111733 if @writer
17121734 @writer . headers
@@ -1718,62 +1740,84 @@ def headers
17181740 raw_headers
17191741 end
17201742 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
17251747 def return_headers?
17261748 parser . return_headers?
17271749 end
17281750
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
17331754 def write_headers?
17341755 @writer_options [ :write_headers ]
17351756 end
17361757
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 # => []
17421761 def header_converters
17431762 header_fields_converter . map do |converter |
17441763 name = HeaderConverters . rassoc ( converter )
17451764 name ? name . first : converter
17461765 end
17471766 end
17481767
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
17531771 def skip_blanks?
17541772 parser . skip_blanks?
17551773 end
17561774
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
17581779 def force_quotes?
17591780 @writer_options [ :force_quotes ]
17601781 end
17611782
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
17631786 def liberal_parsing?
17641787 parser . liberal_parsing?
17651788 end
17661789
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>
17711793 attr_reader :encoding
17721794
1795+ # Returns the count of the rows parsed or generated.
17731796 #
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
17771821 def lineno
17781822 if @writer
17791823 @writer . lineno
@@ -1782,9 +1826,19 @@ def lineno
17821826 end
17831827 end
17841828
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"]
17881842 def line
17891843 parser . line
17901844 end
0 commit comments