diff options
Diffstat (limited to 'doc/csv/options/common/quote_char.rdoc')
-rw-r--r-- | doc/csv/options/common/quote_char.rdoc | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/doc/csv/options/common/quote_char.rdoc b/doc/csv/options/common/quote_char.rdoc index eb2ad9c0c8..67fd3af68b 100644 --- a/doc/csv/options/common/quote_char.rdoc +++ b/doc/csv/options/common/quote_char.rdoc @@ -5,28 +5,38 @@ in both parsing and generating. This String will be transcoded into the data's \Encoding before use. Default value: - CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (backslash) + CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote) This is useful for an application that incorrectly uses <tt>'</tt> (single-quote) to quote fields, instead of the correct <tt>"</tt> (double-quote). -Using the default: - ary = ['a', 'b', '"c"', 'd'] - str = CSV.generate_line(ary) - str # => "a,b,\"\"\"c\"\"\",d\n" - ary = CSV.parse_line(str) - ary # => ["a", "b", "\"c\"", "d"] +Using the default (double quote): + str = CSV.generate do |csv| + csv << ['foo', 0] + csv << ["'bar'", 1] + csv << ['"baz"', 2] + end + str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n" + ary = CSV.parse(str) + ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]] Using <tt>'</tt> (single-quote): quote_char = "'" - ary = ['a', 'b', '\'c\'', 'd'] - str = CSV.generate_line(ary, quote_char: quote_char) - str # => "a,b,'''c''',d\n" - ary = CSV.parse_line(str, quote_char: quote_char) - ary # => [["a", "b", "'c'", "d"]] + str = CSV.generate(quote_char: quote_char) do |csv| + csv << ['foo', 0] + csv << ["'bar'", 1] + csv << ['"baz"', 2] + end + str # => "foo,0\n'''bar''',1\n\"baz\",2\n" + ary = CSV.parse(str, quote_char: quote_char) + ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]] --- Raises an exception if the \String length is greater than 1: # Raises ArgumentError (:quote_char has to be nil or a single character String) CSV.new('', quote_char: 'xx') + +Raises an exception if the value is not a \String: + # Raises ArgumentError (:quote_char has to be nil or a single character String) + CSV.new('', quote_char: :foo) |