Skip to content

Commit 223cbee

Browse files
Improve RDoc for common options (#146)
1 parent bc9ea85 commit 223cbee

File tree

3 files changed

+74
-37
lines changed

3 files changed

+74
-37
lines changed

doc/options/common/col_sep.rdoc

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,57 @@ The \String will be transcoded into the data's \Encoding before use.
77
Default value:
88
CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
99

10-
For examples in this section:
11-
ary = ['a', 'b', 'c']
12-
13-
Using the default:
14-
str = CSV.generate_line(line)
15-
str # => "a,b,c\n"
16-
ary = CSV.parse_line(str)
17-
ary # => ["a", "b", "c"]
10+
Using the default (comma):
11+
str = CSV.generate do |csv|
12+
csv << [:foo, 0]
13+
csv << [:bar, 1]
14+
csv << [:baz, 2]
15+
end
16+
str # => "foo,0\nbar,1\nbaz,2\n"
17+
ary = CSV.parse(str)
18+
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1819

1920
Using +:+ (colon):
2021
col_sep = ':'
21-
str = CSV.generate_line(ary, col_sep: col_sep)
22-
str # => "a:b:c\n"
23-
ary = CSV.parse_line(str, col_sep: col_sep)
24-
ary # => [["a", "b", "c"]]
22+
str = CSV.generate(col_sep: col_sep) do |csv|
23+
csv << [:foo, 0]
24+
csv << [:bar, 1]
25+
csv << [:baz, 2]
26+
end
27+
str # => "foo:0\nbar:1\nbaz:2\n"
28+
ary = CSV.parse(str, col_sep: col_sep)
29+
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
2530

2631
Using +::+ (two colons):
2732
col_sep = '::'
28-
str = CSV.generate_line(ary, col_sep: col_sep)
29-
str # => "a::b::c\n"
30-
ary = CSV.parse_line(str, col_sep: col_sep)
31-
ary # => [["a", "b", "c"]]
33+
str = CSV.generate(col_sep: col_sep) do |csv|
34+
csv << [:foo, 0]
35+
csv << [:bar, 1]
36+
csv << [:baz, 2]
37+
end
38+
str # => "foo::0\nbar::1\nbaz::2\n"
39+
ary = CSV.parse(str, col_sep: col_sep)
40+
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
41+
42+
Using <tt>''</tt> (empty string):
43+
col_sep = ''
44+
str = CSV.generate(col_sep: col_sep) do |csv|
45+
csv << [:foo, 0]
46+
csv << [:bar, 1]
47+
csv << [:baz, 2]
48+
end
49+
str # => "foo0\nbar1\nbaz2\n"
3250

3351
---
3452

35-
Raises an exception if given the empty \String:
53+
Raises an exception if parsing with the empty \String:
3654
col_sep = ''
3755
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
38-
CSV.parse_line("a:b:c\n", col_sep: col_sep)
56+
CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
3957

4058
Raises an exception if the given value is not String-convertible:
4159
col_sep = BasicObject.new
4260
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
43-
CSV.generate_line(line, col_sep: col_sep)
61+
CSV.generate(line, col_sep: col_sep)
4462
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
4563
CSV.parse(str, col_sep: col_sep)

doc/options/common/quote_char.rdoc

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,38 @@ in both parsing and generating.
55
This String will be transcoded into the data's \Encoding before use.
66

77
Default value:
8-
CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (backslash)
8+
CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote)
99

1010
This is useful for an application that incorrectly uses <tt>'</tt> (single-quote)
1111
to quote fields, instead of the correct <tt>"</tt> (double-quote).
1212

13-
Using the default:
14-
ary = ['a', 'b', '"c"', 'd']
15-
str = CSV.generate_line(ary)
16-
str # => "a,b,\"\"\"c\"\"\",d\n"
17-
ary = CSV.parse_line(str)
18-
ary # => ["a", "b", "\"c\"", "d"]
13+
Using the default (double quote):
14+
str = CSV.generate do |csv|
15+
csv << ['foo', 0]
16+
csv << ["'bar'", 1]
17+
csv << ['"baz"', 2]
18+
end
19+
str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n"
20+
ary = CSV.parse(str)
21+
ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
1922

2023
Using <tt>'</tt> (single-quote):
2124
quote_char = "'"
22-
ary = ['a', 'b', '\'c\'', 'd']
23-
str = CSV.generate_line(ary, quote_char: quote_char)
24-
str # => "a,b,'''c''',d\n"
25-
ary = CSV.parse_line(str, quote_char: quote_char)
26-
ary # => [["a", "b", "'c'", "d"]]
25+
str = CSV.generate(quote_char: quote_char) do |csv|
26+
csv << ['foo', 0]
27+
csv << ["'bar'", 1]
28+
csv << ['"baz"', 2]
29+
end
30+
str # => "foo,0\n'''bar''',1\n\"baz\",2\n"
31+
ary = CSV.parse(str, quote_char: quote_char)
32+
ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
2733

2834
---
2935

3036
Raises an exception if the \String length is greater than 1:
3137
# Raises ArgumentError (:quote_char has to be nil or a single character String)
3238
CSV.new('', quote_char: 'xx')
39+
40+
Raises an exception if the value is not a \String:
41+
# Raises ArgumentError (:quote_char has to be nil or a single character String)
42+
CSV.new('', quote_char: :foo)

doc/options/common/row_sep.rdoc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ When +row_sep+ is a \String, that \String becomes the row separator.
1212
The String will be transcoded into the data's Encoding before use.
1313

1414
Using <tt>"\n"</tt>:
15-
str = CSV.generate do |csv|
15+
row_sep = "\n"
16+
str = CSV.generate(row_sep: row_sep) do |csv|
1617
csv << [:foo, 0]
1718
csv << [:bar, 1]
1819
csv << [:baz, 2]
@@ -57,20 +58,28 @@ Using <tt>''</tt> (empty string):
5758
---
5859

5960
When +row_sep+ is the \Symbol +:auto+ (the default),
60-
invokes auto-discovery of the row separator.
61+
generating uses <tt>"\n"</tt> as the row separator:
62+
str = CSV.generate do |csv|
63+
csv << [:foo, 0]
64+
csv << [:bar, 1]
65+
csv << [:baz, 2]
66+
end
67+
str # => "foo,0\nbar,1\nbaz,2\n"
68+
69+
Parsing, on the other hand, invokes auto-discovery of the row separator.
6170

6271
Auto-discovery reads ahead in the data looking for the next <tt>\r\n</tt>, +\n+, or +\r+ sequence.
6372
The sequence will be selected even if it occurs in a quoted field,
6473
assuming that you would have the same line endings there.
6574

66-
row_sep = :auto
67-
str = CSV.generate(row_sep: row_sep) do |csv|
75+
Example:
76+
str = CSV.generate do |csv|
6877
csv << [:foo, 0]
6978
csv << [:bar, 1]
7079
csv << [:baz, 2]
7180
end
7281
str # => "foo,0\nbar,1\nbaz,2\n"
73-
ary = CSV.parse(str, row_sep: row_sep)
82+
ary = CSV.parse(str)
7483
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
7584

7685
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
8695
Raises an exception if the given value is not String-convertible:
8796
row_sep = BasicObject.new
8897
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
89-
CSV.generate_line(ary, row_sep: row_sep)
98+
CSV.generate(ary, row_sep: row_sep)
9099
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
91100
CSV.parse(str, row_sep: row_sep)

0 commit comments

Comments
 (0)