Skip to content

Commit cd67059

Browse files
Enhanced Rdoc for CSV (#122)
1 parent c49cba0 commit cd67059

22 files changed

+962
-216
lines changed

doc/col_sep.rdoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
====== Option +col_sep+
2+
3+
Specifies the \String field separator to be used
4+
for both parsing and generating.
5+
The \String will be transcoded into the data's \Encoding before use.
6+
7+
Default value:
8+
CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
9+
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"]
18+
19+
Using +:+ (colon):
20+
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"]]
25+
26+
Using +::+ (two colons):
27+
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"]]
32+
33+
---
34+
35+
Raises an exception if given the empty \String:
36+
col_sep = ''
37+
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
38+
CSV.parse_line("a:b:c\n", col_sep: col_sep)
39+
40+
Raises an exception if the given value is not String-convertible:
41+
col_sep = BasicObject.new
42+
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
43+
CSV.generate_line(line, col_sep: col_sep)
44+
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
45+
CSV.parse(str, col_sep: col_sep)

doc/converters.rdoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
====== Option +converters+
2+
3+
Specifies a single field converter name or \Proc,
4+
or an \Array of field converter names and Procs.
5+
6+
See {Field Converters}[#class-CSV-label-Field+Converters]
7+
8+
Default value:
9+
CSV::DEFAULT_OPTIONS.fetch(:converters) # => nil
10+
11+
The value may be a single field converter name:
12+
str = '1,2,3'
13+
# Without a converter
14+
ary = CSV.parse_line(str)
15+
ary # => ["1", "2", "3"]
16+
# With built-in converter :integer
17+
ary = CSV.parse_line(str, converters: :integer)
18+
ary # => [1, 2, 3]
19+
20+
The value may be an \Array of field converter names:
21+
str = '1,3.14159'
22+
# Without converters
23+
ary = CSV.parse_line(str)
24+
ary # => ["1", "3.14159"]
25+
# With built-in converters
26+
ary = CSV.parse_line(str, converters: [:integer, :float])
27+
ary # => [1, 3.14159]
28+
29+
The value may be a \Proc custom converter:
30+
str = ' foo , bar , baz '
31+
# Without a converter
32+
ary = CSV.parse_line(str)
33+
ary # => [" foo ", " bar ", " baz "]
34+
# With a custom converter
35+
ary = CSV.parse_line(str, converters: proc {|field| field.strip })
36+
ary # => ["foo", "bar", "baz"]
37+
38+
See also {Custom Converters}[#class-CSV-label-Custom+Converters]
39+
40+
---
41+
42+
Raises an exception if the converter is not a converter name or a \Proc:
43+
str = 'foo,0'
44+
# Raises NoMethodError (undefined method `arity' for nil:NilClass)
45+
CSV.parse(str, converters: :foo)

doc/empty_value.rdoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
====== Option +empty_value+
2+
3+
Specifies the object that is to be substituted
4+
for each field that has an empty \String.
5+
6+
Default value:
7+
CSV::DEFAULT_OPTIONS.fetch(:empty_value) # => "" (empty string)
8+
9+
With the default, <tt>""</tt>:
10+
CSV.parse_line('a,"",b,"",c') # => ["a", "", "b", "", "c"]
11+
12+
With a different object:
13+
CSV.parse_line('a,"",b,"",c', empty_value: 'x') # => ["a", "x", "b", "x", "c"]

doc/field_size_limit.rdoc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
====== Option +field_size_limit+
2+
3+
Specifies the \Integer field size limit.
4+
5+
Default value:
6+
CSV::DEFAULT_OPTIONS.fetch(:field_size_limit) # => nil
7+
8+
This is a maximum size CSV will read ahead looking for the closing quote for a field.
9+
(In truth, it reads to the first line ending beyond this size.)
10+
If a quote cannot be found within the limit CSV will raise a MalformedCSVError,
11+
assuming the data is faulty.
12+
You can use this limit to prevent what are effectively DoS attacks on the parser.
13+
However, this limit can cause a legitimate parse to fail;
14+
therefore the default value is +nil+ (no limit).
15+
16+
For the examples in this section:
17+
str = <<~EOT
18+
"a","b"
19+
"
20+
2345
21+
",""
22+
EOT
23+
str # => "\"a\",\"b\"\n\"\n2345\n\",\"\"\n"
24+
25+
Using the default +nil+:
26+
ary = CSV.parse(str)
27+
ary # => [["a", "b"], ["\n2345\n", ""]]
28+
29+
Using <tt>50</tt>:
30+
field_size_limit = 50
31+
ary = CSV.parse(str, field_size_limit: field_size_limit)
32+
ary # => [["a", "b"], ["\n2345\n", ""]]
33+
34+
---
35+
36+
Raises an exception if a field is too long:
37+
big_str = "123456789\n" * 1024
38+
# Raises CSV::MalformedCSVError (Field size exceeded in line 1.)
39+
CSV.parse('valid,fields,"' + big_str + '"', field_size_limit: 2048)

doc/force_quotes.rdoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
====== Option +force_quotes+
2+
3+
Specifies the boolean that determines whether each output field is to be double-quoted.
4+
5+
Default value:
6+
CSV::DEFAULT_OPTIONS.fetch(:force_quotes) # => false
7+
8+
For examples in this section:
9+
ary = ['foo', 0, nil]
10+
11+
Using the default, +false+:
12+
str = CSV.generate_line(ary)
13+
str # => "foo,0,\n"
14+
15+
Using +true+:
16+
str = CSV.generate_line(ary, force_quotes: true)
17+
str # => "\"foo\",\"0\",\"\"\n"

doc/header_converters.rdoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
====== Option +header_converters+
2+
3+
Specifies a \String converter name or an \Array of converter names.
4+
5+
Default value:
6+
CSV::DEFAULT_OPTIONS.fetch(:header_converters) # => nil
7+
8+
Identical in functionality to option {converters}[#class-CSV-label-Option+converters]
9+
except that:
10+
- The converters apply only to the header row.
11+
- The built-in header converters are +:downcase+ and +:symbol+.
12+
13+
Examples:
14+
str = <<-EOT
15+
foo,0
16+
bar,1
17+
baz,2
18+
EOT
19+
headers = ['Name', 'Value']
20+
# With no header converter
21+
csv = CSV.parse(str, headers: headers)
22+
csv.headers # => ["Name", "Value"]
23+
# With header converter :downcase
24+
csv = CSV.parse(str, headers: headers, header_converters: :downcase)
25+
csv.headers # => ["name", "value"]
26+
# With header converter :symbol
27+
csv = CSV.parse(str, headers: headers, header_converters: :symbol)
28+
csv.headers # => [:name, :value]
29+
# With both
30+
csv = CSV.parse(str, headers: headers, header_converters: [:downcase, :symbol])
31+
csv.headers # => [:name, :value]

doc/headers.rdoc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
====== Option +headers+
2+
3+
Specifies a boolean, \Symbol, \Array, or \String to be used
4+
to define column headers.
5+
6+
Default value:
7+
CSV::DEFAULT_OPTIONS.fetch(:headers) # => false
8+
9+
---
10+
11+
Without +headers+:
12+
str = <<-EOT
13+
Name,Count
14+
foo,0
15+
bar,1
16+
bax,2
17+
EOT
18+
csv = CSV.new(str)
19+
csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
20+
csv.headers # => nil
21+
csv.shift # => ["Name", "Count"]
22+
23+
---
24+
25+
If set to +true+ or the \Symbol +:first_row+,
26+
the first row of the data is treated as a row of headers:
27+
str = <<-EOT
28+
Name,Count
29+
foo,0
30+
bar,1
31+
bax,2
32+
EOT
33+
csv = CSV.new(str, headers: true)
34+
csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\n" quote_char:"\"" headers:["Name", "Count"]>
35+
csv.headers # => ["Name", "Count"]
36+
csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
37+
38+
---
39+
40+
If set to an \Array, the \Array elements are treated as headers:
41+
str = <<-EOT
42+
foo,0
43+
bar,1
44+
bax,2
45+
EOT
46+
csv = CSV.new(str, headers: ['Name', 'Count'])
47+
csv
48+
csv.headers # => ["Name", "Count"]
49+
csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
50+
51+
---
52+
53+
If set to a \String +str+, method <tt>CSV::parse_line(str, options)</tt> is called
54+
with the current +options+, and the returned \Array is treated as headers:
55+
str = <<-EOT
56+
foo,0
57+
bar,1
58+
bax,2
59+
EOT
60+
csv = CSV.new(str, headers: 'Name,Count')
61+
csv
62+
csv.headers # => ["Name", "Count"]
63+
csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">

doc/liberal_parsing.rdoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
====== Option +liberal_parsing+
2+
3+
Specifies the boolean value that determines whether
4+
CSV will attempt to parse input not conformant with RFC 4180,
5+
such as double quotes in unquoted fields.
6+
7+
Default value:
8+
CSV::DEFAULT_OPTIONS.fetch(:liberal_parsing) # => false
9+
10+
For examples in this section:
11+
str = 'is,this "three, or four",fields'
12+
13+
Without +liberal_parsing+:
14+
# Raises CSV::MalformedCSVError (Illegal quoting in str 1.)
15+
CSV.parse_line(str)
16+
17+
With +liberal_parsing+:
18+
ary = CSV.parse_line(str, liberal_parsing: true)
19+
ary # => ["is", "this \"three", " or four\"", "fields"]

doc/nil_value.rdoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
====== Option +nil_value+
2+
3+
Specifies the object that is to be substituted for each null (no-text) field.
4+
5+
Default value:
6+
CSV::DEFAULT_OPTIONS.fetch(:nil_value) # => nil
7+
8+
With the default, +nil+:
9+
CSV.parse_line('a,,b,,c') # => ["a", nil, "b", nil, "c"]
10+
11+
With a different object:
12+
CSV.parse_line('a,,b,,c', nil_value: 0) # => ["a", 0, "b", 0, "c"]

doc/quote_char.rdoc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
====== Option +quote_char+
2+
3+
Specifies the character (\String of length 1) used used to quote fields
4+
in both parsing and generating.
5+
This String will be transcoded into the data's \Encoding before use.
6+
7+
Default value:
8+
CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (backslash)
9+
10+
This is useful for an application that incorrectly uses <tt>'</tt> (single-quote)
11+
to quote fields, instead of the correct <tt>"</tt> (double-quote).
12+
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"]
19+
20+
Using <tt>'</tt> (single-quote):
21+
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"]]
27+
28+
---
29+
30+
Raises an exception if the \String length is greater than 1:
31+
# Raises ArgumentError (:quote_char has to be nil or a single character String)
32+
CSV.new('', quote_char: 'xx')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy