mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
8ea293b79c
commit
98d52d873e
Notes:
git
2020-11-24 09:34:31 +09:00
1 changed files with 76 additions and 45 deletions
|
@ -1,33 +1,46 @@
|
||||||
== Recipes
|
== Recipes
|
||||||
|
|
||||||
=== Contents
|
=== 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
|
- {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]
|
||||||
|
|
||||||
==== Parse from \String Without Headers
|
=== Parsing: Source Formats
|
||||||
|
|
||||||
|
==== Parse from \String
|
||||||
|
|
||||||
|
===== Parse from \String Without Headers
|
||||||
|
|
||||||
\Class method CSV.parse can read a source \String all at once,
|
\Class method CSV.parse can read a source \String all at once,
|
||||||
and so may have memory resource implications:
|
and so may have memory resource implications:
|
||||||
|
@ -43,7 +56,7 @@ Output:
|
||||||
["bar", "1"]
|
["bar", "1"]
|
||||||
["baz", "2"]
|
["baz", "2"]
|
||||||
|
|
||||||
==== Parse from \String with Headers
|
===== Parse from \String with Headers
|
||||||
|
|
||||||
\Class method CSV.parse can read a source \String all at once,
|
\Class method CSV.parse can read a source \String all at once,
|
||||||
and so may have memory resource implications:
|
and so may have memory resource implications:
|
||||||
|
@ -59,7 +72,9 @@ Ouput:
|
||||||
#<CSV::Row "Name":"bar" "Value":"1">
|
#<CSV::Row "Name":"bar" "Value":"1">
|
||||||
#<CSV::Row "Name":"baz" "Value":"2">
|
#<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:
|
\Class method CSV.read can read a file all at once:
|
||||||
string = "foo,0\nbar,1\nbaz,2\n"
|
string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
|
@ -76,7 +91,7 @@ Output:
|
||||||
["bar", "1"]
|
["bar", "1"]
|
||||||
["baz", "2"]
|
["baz", "2"]
|
||||||
|
|
||||||
==== Parse from \File with Headers
|
===== Parse from \File with Headers
|
||||||
|
|
||||||
Instance method CSV#read can reada file all at once:
|
Instance method CSV#read can reada file all at once:
|
||||||
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
|
@ -93,7 +108,9 @@ Output:
|
||||||
#<CSV::Row "Name":"bar" "Value":"1">
|
#<CSV::Row "Name":"bar" "Value":"1">
|
||||||
#<CSV::Row "Name":"baz" "Value":"2">
|
#<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:
|
\Class method CSV.parse can read an \IO stream all at once:
|
||||||
string = "foo,0\nbar,1\nbaz,2\n"
|
string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
|
@ -114,7 +131,7 @@ Output:
|
||||||
["bar", "1"]
|
["bar", "1"]
|
||||||
["baz", "2"]
|
["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:
|
\Class method CSV.parse can read an \IO stream all at once:
|
||||||
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
|
@ -135,7 +152,7 @@ Output:
|
||||||
#<CSV::Row "Name":"bar" "Value":"1">
|
#<CSV::Row "Name":"bar" "Value":"1">
|
||||||
#<CSV::Row "Name":"baz" "Value":"2">
|
#<CSV::Row "Name":"baz" "Value":"2">
|
||||||
|
|
||||||
=== Generating
|
=== Generating: Output Formats
|
||||||
|
|
||||||
==== Generate to \String Without Headers
|
==== Generate to \String Without Headers
|
||||||
|
|
||||||
|
@ -150,7 +167,9 @@ that are to be generated:
|
||||||
end
|
end
|
||||||
output_string # => "Foo,0\nBar,1\nBaz,2\n"
|
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.
|
\Class method CSV.generate can generate to a \String.
|
||||||
|
|
||||||
|
@ -163,7 +182,7 @@ that are to be generated:
|
||||||
end
|
end
|
||||||
output_string # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
|
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.
|
\Class method CSV.open can generate to a \File.
|
||||||
|
|
||||||
|
@ -177,7 +196,9 @@ that are to be generated:
|
||||||
end
|
end
|
||||||
p File.read(path) # => "Foo,0\nBar,1\nBaz,2\n"
|
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.
|
\Class method CSV.open can generate to a \File.
|
||||||
|
|
||||||
|
@ -191,7 +212,9 @@ that are to be generated:
|
||||||
end
|
end
|
||||||
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
|
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:
|
\Class method CSV.new can generate \CSV data to an \IO stream:
|
||||||
path = 't.csv'
|
path = 't.csv'
|
||||||
|
@ -215,12 +238,14 @@ that are to be generated:
|
||||||
end
|
end
|
||||||
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
|
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.
|
\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 Without Headers
|
==== Filter \String to \String
|
||||||
|
|
||||||
|
===== Filter \String to \String Without Headers
|
||||||
|
|
||||||
in_string = "foo,0\nbar,1\nbaz,2\n"
|
in_string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
out_string = ''
|
out_string = ''
|
||||||
|
@ -230,7 +255,7 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
|
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"
|
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
out_string = ''
|
out_string = ''
|
||||||
|
@ -240,7 +265,9 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
|
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"
|
in_string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
path = 't.csv'
|
path = 't.csv'
|
||||||
|
@ -252,7 +279,7 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
p File.read(path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
|
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"
|
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
path = 't.csv'
|
path = 't.csv'
|
||||||
|
@ -264,7 +291,9 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
p File.read(path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
|
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"
|
in_string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
path = 't.csv'
|
path = 't.csv'
|
||||||
|
@ -278,7 +307,7 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
|
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"
|
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
path = 't.csv'
|
path = 't.csv'
|
||||||
|
@ -292,7 +321,9 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
|
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_path = 't.csv'
|
||||||
in_string = "foo,0\nbar,1\nbaz,2\n"
|
in_string = "foo,0\nbar,1\nbaz,2\n"
|
||||||
|
@ -308,7 +339,7 @@ The input \CSV data is processed to form output \CSV data.
|
||||||
end
|
end
|
||||||
p File.read(out_path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
|
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_path = 't.csv'
|
||||||
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue