[ruby/csv] Improve RDoc for common options (#146)
https://github.com/ruby/csv/commit/223cbee35d
This commit is contained in:
parent
d9eff306f5
commit
7bf13c5183
Notes:
git
2020-07-20 03:35:35 +09:00
@ -7,39 +7,57 @@ The \String will be transcoded into the data's \Encoding before use.
|
|||||||
Default value:
|
Default value:
|
||||||
CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
|
CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
|
||||||
|
|
||||||
For examples in this section:
|
Using the default (comma):
|
||||||
ary = ['a', 'b', 'c']
|
str = CSV.generate do |csv|
|
||||||
|
csv << [:foo, 0]
|
||||||
Using the default:
|
csv << [:bar, 1]
|
||||||
str = CSV.generate_line(line)
|
csv << [:baz, 2]
|
||||||
str # => "a,b,c\n"
|
end
|
||||||
ary = CSV.parse_line(str)
|
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||||
ary # => ["a", "b", "c"]
|
ary = CSV.parse(str)
|
||||||
|
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||||
|
|
||||||
Using +:+ (colon):
|
Using +:+ (colon):
|
||||||
col_sep = ':'
|
col_sep = ':'
|
||||||
str = CSV.generate_line(ary, col_sep: col_sep)
|
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||||
str # => "a:b:c\n"
|
csv << [:foo, 0]
|
||||||
ary = CSV.parse_line(str, col_sep: col_sep)
|
csv << [:bar, 1]
|
||||||
ary # => [["a", "b", "c"]]
|
csv << [:baz, 2]
|
||||||
|
end
|
||||||
|
str # => "foo:0\nbar:1\nbaz:2\n"
|
||||||
|
ary = CSV.parse(str, col_sep: col_sep)
|
||||||
|
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||||
|
|
||||||
Using +::+ (two colons):
|
Using +::+ (two colons):
|
||||||
col_sep = '::'
|
col_sep = '::'
|
||||||
str = CSV.generate_line(ary, col_sep: col_sep)
|
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||||
str # => "a::b::c\n"
|
csv << [:foo, 0]
|
||||||
ary = CSV.parse_line(str, col_sep: col_sep)
|
csv << [:bar, 1]
|
||||||
ary # => [["a", "b", "c"]]
|
csv << [:baz, 2]
|
||||||
|
end
|
||||||
|
str # => "foo::0\nbar::1\nbaz::2\n"
|
||||||
|
ary = CSV.parse(str, col_sep: col_sep)
|
||||||
|
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||||
|
|
||||||
|
Using <tt>''</tt> (empty string):
|
||||||
|
col_sep = ''
|
||||||
|
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||||
|
csv << [:foo, 0]
|
||||||
|
csv << [:bar, 1]
|
||||||
|
csv << [:baz, 2]
|
||||||
|
end
|
||||||
|
str # => "foo0\nbar1\nbaz2\n"
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Raises an exception if given the empty \String:
|
Raises an exception if parsing with the empty \String:
|
||||||
col_sep = ''
|
col_sep = ''
|
||||||
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
|
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
|
||||||
CSV.parse_line("a:b:c\n", col_sep: col_sep)
|
CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
|
||||||
|
|
||||||
Raises an exception if the given value is not String-convertible:
|
Raises an exception if the given value is not String-convertible:
|
||||||
col_sep = BasicObject.new
|
col_sep = BasicObject.new
|
||||||
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
||||||
CSV.generate_line(line, col_sep: col_sep)
|
CSV.generate(line, col_sep: col_sep)
|
||||||
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
||||||
CSV.parse(str, col_sep: col_sep)
|
CSV.parse(str, col_sep: col_sep)
|
||||||
|
@ -5,28 +5,38 @@ in both parsing and generating.
|
|||||||
This String will be transcoded into the data's \Encoding before use.
|
This String will be transcoded into the data's \Encoding before use.
|
||||||
|
|
||||||
Default value:
|
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)
|
This is useful for an application that incorrectly uses <tt>'</tt> (single-quote)
|
||||||
to quote fields, instead of the correct <tt>"</tt> (double-quote).
|
to quote fields, instead of the correct <tt>"</tt> (double-quote).
|
||||||
|
|
||||||
Using the default:
|
Using the default (double quote):
|
||||||
ary = ['a', 'b', '"c"', 'd']
|
str = CSV.generate do |csv|
|
||||||
str = CSV.generate_line(ary)
|
csv << ['foo', 0]
|
||||||
str # => "a,b,\"\"\"c\"\"\",d\n"
|
csv << ["'bar'", 1]
|
||||||
ary = CSV.parse_line(str)
|
csv << ['"baz"', 2]
|
||||||
ary # => ["a", "b", "\"c\"", "d"]
|
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):
|
Using <tt>'</tt> (single-quote):
|
||||||
quote_char = "'"
|
quote_char = "'"
|
||||||
ary = ['a', 'b', '\'c\'', 'd']
|
str = CSV.generate(quote_char: quote_char) do |csv|
|
||||||
str = CSV.generate_line(ary, quote_char: quote_char)
|
csv << ['foo', 0]
|
||||||
str # => "a,b,'''c''',d\n"
|
csv << ["'bar'", 1]
|
||||||
ary = CSV.parse_line(str, quote_char: quote_char)
|
csv << ['"baz"', 2]
|
||||||
ary # => [["a", "b", "'c'", "d"]]
|
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 an exception if the \String length is greater than 1:
|
||||||
# Raises ArgumentError (:quote_char has to be nil or a single character String)
|
# Raises ArgumentError (:quote_char has to be nil or a single character String)
|
||||||
CSV.new('', quote_char: 'xx')
|
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)
|
||||||
|
@ -12,7 +12,8 @@ When +row_sep+ is a \String, that \String becomes the row separator.
|
|||||||
The String will be transcoded into the data's Encoding before use.
|
The String will be transcoded into the data's Encoding before use.
|
||||||
|
|
||||||
Using <tt>"\n"</tt>:
|
Using <tt>"\n"</tt>:
|
||||||
str = CSV.generate do |csv|
|
row_sep = "\n"
|
||||||
|
str = CSV.generate(row_sep: row_sep) do |csv|
|
||||||
csv << [:foo, 0]
|
csv << [:foo, 0]
|
||||||
csv << [:bar, 1]
|
csv << [:bar, 1]
|
||||||
csv << [:baz, 2]
|
csv << [:baz, 2]
|
||||||
@ -57,20 +58,28 @@ Using <tt>''</tt> (empty string):
|
|||||||
---
|
---
|
||||||
|
|
||||||
When +row_sep+ is the \Symbol +:auto+ (the default),
|
When +row_sep+ is the \Symbol +:auto+ (the default),
|
||||||
invokes auto-discovery of the row separator.
|
generating uses <tt>"\n"</tt> as the row separator:
|
||||||
|
str = CSV.generate do |csv|
|
||||||
Auto-discovery reads ahead in the data looking for the next <tt>\r\n</tt>, +\n+, or +\r+ sequence.
|
|
||||||
The sequence will be selected even if it occurs in a quoted field,
|
|
||||||
assuming that you would have the same line endings there.
|
|
||||||
|
|
||||||
row_sep = :auto
|
|
||||||
str = CSV.generate(row_sep: row_sep) do |csv|
|
|
||||||
csv << [:foo, 0]
|
csv << [:foo, 0]
|
||||||
csv << [:bar, 1]
|
csv << [:bar, 1]
|
||||||
csv << [:baz, 2]
|
csv << [:baz, 2]
|
||||||
end
|
end
|
||||||
str # => "foo,0\nbar,1\nbaz,2\n"
|
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||||
ary = CSV.parse(str, row_sep: row_sep)
|
|
||||||
|
Parsing, on the other hand, invokes auto-discovery of the row separator.
|
||||||
|
|
||||||
|
Auto-discovery reads ahead in the data looking for the next <tt>\r\n</tt>, +\n+, or +\r+ sequence.
|
||||||
|
The sequence will be selected even if it occurs in a quoted field,
|
||||||
|
assuming that you would have the same line endings there.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
str = CSV.generate do |csv|
|
||||||
|
csv << [:foo, 0]
|
||||||
|
csv << [:bar, 1]
|
||||||
|
csv << [:baz, 2]
|
||||||
|
end
|
||||||
|
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||||
|
ary = CSV.parse(str)
|
||||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||||
|
|
||||||
The default <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) is used
|
The default <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) is used
|
||||||
@ -86,6 +95,6 @@ Obviously, discovery takes a little time. Set manually if speed is important. Al
|
|||||||
Raises an exception if the given value is not String-convertible:
|
Raises an exception if the given value is not String-convertible:
|
||||||
row_sep = BasicObject.new
|
row_sep = BasicObject.new
|
||||||
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
||||||
CSV.generate_line(ary, row_sep: row_sep)
|
CSV.generate(ary, row_sep: row_sep)
|
||||||
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
|
||||||
CSV.parse(str, row_sep: row_sep)
|
CSV.parse(str, row_sep: row_sep)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user