Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 78 additions & 47 deletions doc/csv/recipes.rdoc
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
== Recipes

=== Contents
- {Parsing}[#label-Parsing]
- {Parse from String Without Headers}[#label-Parse+from+String+Without+Headers]
- {Parse from String with Headers}[#label-Parse+from+String+with+Headers]
- {Parse from File Without Headers}[#label-Parse+from+File+Without+Headers]
- {Parse from File with Headers}[#label-Parse+from+File+with+Headers]
- {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
- {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers]
- {Generating}[#label-Generating]
- {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers]
- {Generate to String with Headers}[#label-Generate+to+String+with+Headers]
- {Generate to File Without Headers}[#label-Generate+to+File+Without+Headers]
- {Generate to File with Headers}[#label-Generate+to+File+with+Headers]
- {Generate to IO Stream Without Headers}[#label-Generate+to+IO+Stream+Without+Headers]
- {Generate to IO Stream with Headers}[#label-Generate+to+IO+Stream+with+Headers]
- {Filtering}[#label-Filtering]
- {Filter String to String Without Headers}[#label-Filter+String+to+String+Without+Headers]
- {Filter String to String with Headers}[#label-Filter+String+to+String+with+Headers]
- {Filter String to IO Stream Without Headers}[#label-Filter+String+to+IO+Stream+Without+Headers]
- {Filter String to IO Stream with Headers}[#label-Filter+String+to+IO+Stream+with+Headers]
- {Filter IO Stream to String Without Headers}[#label-Filter+IO+Stream+to+String+Without+Headers]
- {Filter IO Stream to String with Headers}[#label-Filter+IO+Stream+to+String+with+Headers]
- {Filter IO Stream to IO Stream Without Headers}[#label-Filter+IO+Stream+to+IO+Stream+Without+Headers]
- {Filter IO Stream to IO Stream with Headers}[#label-Filter+IO+Stream+to+IO+Stream+with+Headers]

=== Parsing

==== Parse from \String Without Headers

- {Parsing: Source Formats}[#label-Parsing-3A+Source+Formats]
- {Parse from String}[#label-Parse+from+String]
- {Parse from String Without Headers}[#label-Parse+from+String+Without+Headers]
- {Parse from String with Headers}[#label-Parse+from+String+with+Headers]
- {Parse from File}[#label-Parse+from+File]
- {Parse from File Without Headers}[#label-Parse+from+File+Without+Headers]
- {Parse from File with Headers}[#label-Parse+from+File+with+Headers]
- {Parse from IO Stream}[#label-Parse+from+IO+Stream]
- {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
- {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers]
- {Generating: Output Formats}[#label-Generating-3A+Output+Formats]
- {Generate to String}[#label-Generate+to+String]
- {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers]
- {Generate to String with Headers}[#label-Generate+to+String+with+Headers]
- {Generate to File}[#label-Generate+to+File]
- {Generate to File Without Headers}[#label-Generate+to+File+Without+Headers]
- {Generate to File with Headers}[#label-Generate+to+File+with+Headers]
- {Generate to IO Stream}[#label-Generate+to+IO+Stream]
- {Generate to IO Stream Without Headers}[#label-Generate+to+IO+Stream+Without+Headers]
- {Generate to IO Stream with Headers}[#label-Generate+to+IO+Stream+with+Headers]
- {Filtering: Source and Output Formats}[#label-Filtering-3A+Source+and+Output+Formats]
- {Filter String to String}[#label-Filter+String+to+String]
- {Filter String to String Without Headers}[#label-Filter+String+to+String+Without+Headers]
- {Filter String to String with Headers}[#label-Filter+String+to+String+with+Headers]
- {Filter String to IO Stream}[#label-Filter+String+to+IO+Stream]
- {Filter String to IO Stream Without Headers}[#label-Filter+String+to+IO+Stream+Without+Headers]
- {Filter String to IO Stream with Headers}[#label-Filter+String+to+IO+Stream+with+Headers]
- {Filter IO Stream to String}[#label-Filter+IO+Stream+to+String]
- {Filter IO Stream to String Without Headers}[#label-Filter+IO+Stream+to+String+Without+Headers]
- {Filter IO Stream to String with Headers}[#label-Filter+IO+Stream+to+String+with+Headers]
- {Filter IO Stream to IO Stream}[#label-Filter+IO+Stream+to+IO+Stream]
- {Filter IO Stream to IO Stream Without Headers}[#label-Filter+IO+Stream+to+IO+Stream+Without+Headers]
- {Filter IO Stream to IO Stream with Headers}[#label-Filter+IO+Stream+to+IO+Stream+with+Headers]

=== Parsing: Source Formats

==== Parse from \String

===== Parse from \String Without Headers

\Class method CSV.parse can read a source \String all at once,
and so may have memory resource implications:
Expand All @@ -43,7 +56,7 @@ Output:
["bar", "1"]
["baz", "2"]

==== Parse from \String with Headers
===== Parse from \String with Headers

\Class method CSV.parse can read a source \String all at once,
and so may have memory resource implications:
Expand All @@ -59,7 +72,9 @@ Ouput:
#<CSV::Row "Name":"bar" "Value":"1">
#<CSV::Row "Name":"baz" "Value":"2">

==== Parse from \File Without Headers
==== Parse from \File

===== Parse from \File Without Headers

\Class method CSV.read can read a file all at once:
string = "foo,0\nbar,1\nbaz,2\n"
Expand All @@ -76,7 +91,7 @@ Output:
["bar", "1"]
["baz", "2"]

==== Parse from \File with Headers
===== Parse from \File with Headers

Instance method CSV#read can reada file all at once:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
Expand All @@ -93,7 +108,9 @@ Output:
#<CSV::Row "Name":"bar" "Value":"1">
#<CSV::Row "Name":"baz" "Value":"2">

==== Parse from \IO Stream Without Headers
==== Parse from \IO Stream

===== Parse from \IO Stream Without Headers

\Class method CSV.parse can read an \IO stream all at once:
string = "foo,0\nbar,1\nbaz,2\n"
Expand All @@ -114,7 +131,7 @@ Output:
["bar", "1"]
["baz", "2"]

==== Parse from \IO Stream with Headers
===== Parse from \IO Stream with Headers

\Class method CSV.parse can read an \IO stream all at once:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
Expand All @@ -135,7 +152,7 @@ Output:
#<CSV::Row "Name":"bar" "Value":"1">
#<CSV::Row "Name":"baz" "Value":"2">

=== Generating
=== Generating: Output Formats

==== Generate to \String Without Headers

Expand All @@ -150,7 +167,9 @@ that are to be generated:
end
output_string # => "Foo,0\nBar,1\nBaz,2\n"

==== Generate to \String with Headers
==== Generate to \String

===== Generate to \String with Headers

\Class method CSV.generate can generate to a \String.

Expand All @@ -163,7 +182,7 @@ that are to be generated:
end
output_string # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"

==== Generate to \File Without Headers
===== Generate to \File Without Headers

\Class method CSV.open can generate to a \File.

Expand All @@ -177,7 +196,9 @@ that are to be generated:
end
p File.read(path) # => "Foo,0\nBar,1\nBaz,2\n"

==== Generate to \File with Headers
==== Generate to \File

===== Generate to \File with Headers

\Class method CSV.open can generate to a \File.

Expand All @@ -191,7 +212,9 @@ that are to be generated:
end
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"

==== Generate to \IO Stream Without Headers
==== Generate to \IO Stream

===== Generate to \IO Stream Without Headers

\Class method CSV.new can generate \CSV data to an \IO stream:
path = 't.csv'
Expand All @@ -215,12 +238,14 @@ that are to be generated:
end
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"

=== Filtering
=== Filtering: Source and Output Formats

\Class method CSV.filter provides a Unix-style filter for \CSV data.
The input \CSV data is processed to form output \CSV data.
The source \CSV data is processed to form output \CSV data.

==== Filter \String to \String

==== Filter \String to \String Without Headers
===== Filter \String to \String Without Headers

in_string = "foo,0\nbar,1\nbaz,2\n"
out_string = ''
Expand All @@ -230,7 +255,7 @@ The input \CSV data is processed to form output \CSV data.
end
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \String to \String with Headers
===== Filter \String to \String with Headers

in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
out_string = ''
Expand All @@ -240,7 +265,9 @@ The input \CSV data is processed to form output \CSV data.
end
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \String to \IO Stream Without Headers
==== Filter \String to \IO Stream

===== Filter \String to \IO Stream Without Headers

in_string = "foo,0\nbar,1\nbaz,2\n"
path = 't.csv'
Expand All @@ -252,7 +279,7 @@ The input \CSV data is processed to form output \CSV data.
end
p File.read(path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \String to \IO Stream with Headers
===== Filter \String to \IO Stream with Headers

in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
path = 't.csv'
Expand All @@ -264,7 +291,9 @@ The input \CSV data is processed to form output \CSV data.
end
p File.read(path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \IO Stream to \String Without Headers
==== Filter \IO Stream to \String

===== Filter \IO Stream to \String Without Headers

in_string = "foo,0\nbar,1\nbaz,2\n"
path = 't.csv'
Expand All @@ -278,7 +307,7 @@ The input \CSV data is processed to form output \CSV data.
end
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \IO Stream to \String with Headers
===== Filter \IO Stream to \String with Headers

in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
path = 't.csv'
Expand All @@ -292,7 +321,9 @@ The input \CSV data is processed to form output \CSV data.
end
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \IO Stream to \IO Stream Without Headers
==== Filter \IO Stream to \IO Stream

===== Filter \IO Stream to \IO Stream Without Headers

in_path = 't.csv'
in_string = "foo,0\nbar,1\nbaz,2\n"
Expand All @@ -308,7 +339,7 @@ The input \CSV data is processed to form output \CSV data.
end
p File.read(out_path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"

==== Filter \IO Stream to \IO Stream with Headers
===== Filter \IO Stream to \IO Stream with Headers

in_path = 't.csv'
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
Expand Down