1
1
== Recipes
2
2
3
3
=== Contents
4
- - {Parsing}[#label-Parsing]
5
- - {Parse from String Without Headers}[#label-Parse+from+String+Without+Headers]
6
- - {Parse from String with Headers}[#label-Parse+from+String+with+Headers]
7
- - {Parse from File Without Headers}[#label-Parse+from+File+Without+Headers]
8
- - {Parse from File with Headers}[#label-Parse+from+File+with+Headers]
9
- - {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
10
- - {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers]
11
- - {Generating}[#label-Generating]
12
- - {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers]
13
- - {Generate to String with Headers}[#label-Generate+to+String+with+Headers]
14
- - {Generate to File Without Headers}[#label-Generate+to+File+Without+Headers]
15
- - {Generate to File with Headers}[#label-Generate+to+File+with+Headers]
16
- - {Generate to IO Stream Without Headers}[#label-Generate+to+IO+Stream+Without+Headers]
17
- - {Generate to IO Stream with Headers}[#label-Generate+to+IO+Stream+with+Headers]
18
- - {Filtering}[#label-Filtering]
19
- - {Filter String to String Without Headers}[#label-Filter+String+to+String+Without+Headers]
20
- - {Filter String to String with Headers}[#label-Filter+String+to+String+with+Headers]
21
- - {Filter String to IO Stream Without Headers}[#label-Filter+String+to+IO+Stream+Without+Headers]
22
- - {Filter String to IO Stream with Headers}[#label-Filter+String+to+IO+Stream+with+Headers]
23
- - {Filter IO Stream to String Without Headers}[#label-Filter+IO+Stream+to+String+Without+Headers]
24
- - {Filter IO Stream to String with Headers}[#label-Filter+IO+Stream+to+String+with+Headers]
25
- - {Filter IO Stream to IO Stream Without Headers}[#label-Filter+IO+Stream+to+IO+Stream+Without+Headers]
26
- - {Filter IO Stream to IO Stream with Headers}[#label-Filter+IO+Stream+to+IO+Stream+with+Headers]
27
-
28
- === Parsing
29
-
30
- ==== Parse from \String Without Headers
4
+
5
+ - {Parsing: Source Formats}[#label-Parsing-3A+Source+Formats]
6
+ - {Parse from String}[#label-Parse+from+String]
7
+ - {Parse from String Without Headers}[#label-Parse+from+String+Without+Headers]
8
+ - {Parse from String with Headers}[#label-Parse+from+String+with+Headers]
9
+ - {Parse from File}[#label-Parse+from+File]
10
+ - {Parse from File Without Headers}[#label-Parse+from+File+Without+Headers]
11
+ - {Parse from File with Headers}[#label-Parse+from+File+with+Headers]
12
+ - {Parse from IO Stream}[#label-Parse+from+IO+Stream]
13
+ - {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
14
+ - {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers]
15
+ - {Generating: Output Formats}[#label-Generating-3A+Output+Formats]
16
+ - {Generate to String}[#label-Generate+to+String]
17
+ - {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers]
18
+ - {Generate to String with Headers}[#label-Generate+to+String+with+Headers]
19
+ - {Generate to File}[#label-Generate+to+File]
20
+ - {Generate to File Without Headers}[#label-Generate+to+File+Without+Headers]
21
+ - {Generate to File with Headers}[#label-Generate+to+File+with+Headers]
22
+ - {Generate to IO Stream}[#label-Generate+to+IO+Stream]
23
+ - {Generate to IO Stream Without Headers}[#label-Generate+to+IO+Stream+Without+Headers]
24
+ - {Generate to IO Stream with Headers}[#label-Generate+to+IO+Stream+with+Headers]
25
+ - {Filtering: Source and Output Formats}[#label-Filtering-3A+Source+and+Output+Formats]
26
+ - {Filter String to String}[#label-Filter+String+to+String]
27
+ - {Filter String to String Without Headers}[#label-Filter+String+to+String+Without+Headers]
28
+ - {Filter String to String with Headers}[#label-Filter+String+to+String+with+Headers]
29
+ - {Filter String to IO Stream}[#label-Filter+String+to+IO+Stream]
30
+ - {Filter String to IO Stream Without Headers}[#label-Filter+String+to+IO+Stream+Without+Headers]
31
+ - {Filter String to IO Stream with Headers}[#label-Filter+String+to+IO+Stream+with+Headers]
32
+ - {Filter IO Stream to String}[#label-Filter+IO+Stream+to+String]
33
+ - {Filter IO Stream to String Without Headers}[#label-Filter+IO+Stream+to+String+Without+Headers]
34
+ - {Filter IO Stream to String with Headers}[#label-Filter+IO+Stream+to+String+with+Headers]
35
+ - {Filter IO Stream to IO Stream}[#label-Filter+IO+Stream+to+IO+Stream]
36
+ - {Filter IO Stream to IO Stream Without Headers}[#label-Filter+IO+Stream+to+IO+Stream+Without+Headers]
37
+ - {Filter IO Stream to IO Stream with Headers}[#label-Filter+IO+Stream+to+IO+Stream+with+Headers]
38
+
39
+ === Parsing: Source Formats
40
+
41
+ ==== Parse from \String
42
+
43
+ ===== Parse from \String Without Headers
31
44
32
45
\Class method CSV.parse can read a source \String all at once,
33
46
and so may have memory resource implications:
@@ -43,7 +56,7 @@ Output:
43
56
["bar", "1"]
44
57
["baz", "2"]
45
58
46
- ==== Parse from \String with Headers
59
+ ===== Parse from \String with Headers
47
60
48
61
\Class method CSV.parse can read a source \String all at once,
49
62
and so may have memory resource implications:
59
72
#<CSV::Row "Name":"bar" "Value":"1">
60
73
#<CSV::Row "Name":"baz" "Value":"2">
61
74
62
- ==== Parse from \File Without Headers
75
+ ==== Parse from \File
76
+
77
+ ===== Parse from \File Without Headers
63
78
64
79
\Class method CSV.read can read a file all at once:
65
80
string = "foo,0\nbar,1\nbaz,2\n"
@@ -76,7 +91,7 @@ Output:
76
91
["bar", "1"]
77
92
["baz", "2"]
78
93
79
- ==== Parse from \File with Headers
94
+ ===== Parse from \File with Headers
80
95
81
96
Instance method CSV#read can reada file all at once:
82
97
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -93,7 +108,9 @@ Output:
93
108
#<CSV::Row "Name":"bar" "Value":"1">
94
109
#<CSV::Row "Name":"baz" "Value":"2">
95
110
96
- ==== Parse from \IO Stream Without Headers
111
+ ==== Parse from \IO Stream
112
+
113
+ ===== Parse from \IO Stream Without Headers
97
114
98
115
\Class method CSV.parse can read an \IO stream all at once:
99
116
string = "foo,0\nbar,1\nbaz,2\n"
@@ -114,7 +131,7 @@ Output:
114
131
["bar", "1"]
115
132
["baz", "2"]
116
133
117
- ==== Parse from \IO Stream with Headers
134
+ ===== Parse from \IO Stream with Headers
118
135
119
136
\Class method CSV.parse can read an \IO stream all at once:
120
137
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -135,7 +152,7 @@ Output:
135
152
#<CSV::Row "Name":"bar" "Value":"1">
136
153
#<CSV::Row "Name":"baz" "Value":"2">
137
154
138
- === Generating
155
+ === Generating: Output Formats
139
156
140
157
==== Generate to \String Without Headers
141
158
@@ -150,7 +167,9 @@ that are to be generated:
150
167
end
151
168
output_string # => "Foo,0\nBar,1\nBaz,2\n"
152
169
153
- ==== Generate to \String with Headers
170
+ ==== Generate to \String
171
+
172
+ ===== Generate to \String with Headers
154
173
155
174
\Class method CSV.generate can generate to a \String.
156
175
@@ -163,7 +182,7 @@ that are to be generated:
163
182
end
164
183
output_string # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
165
184
166
- ==== Generate to \File Without Headers
185
+ ===== Generate to \File Without Headers
167
186
168
187
\Class method CSV.open can generate to a \File.
169
188
@@ -177,7 +196,9 @@ that are to be generated:
177
196
end
178
197
p File.read(path) # => "Foo,0\nBar,1\nBaz,2\n"
179
198
180
- ==== Generate to \File with Headers
199
+ ==== Generate to \File
200
+
201
+ ===== Generate to \File with Headers
181
202
182
203
\Class method CSV.open can generate to a \File.
183
204
@@ -191,7 +212,9 @@ that are to be generated:
191
212
end
192
213
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
193
214
194
- ==== Generate to \IO Stream Without Headers
215
+ ==== Generate to \IO Stream
216
+
217
+ ===== Generate to \IO Stream Without Headers
195
218
196
219
\Class method CSV.new can generate \CSV data to an \IO stream:
197
220
path = 't.csv'
@@ -215,12 +238,14 @@ that are to be generated:
215
238
end
216
239
p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
217
240
218
- === Filtering
241
+ === Filtering: Source and Output Formats
219
242
220
243
\Class method CSV.filter provides a Unix-style filter for \CSV data.
221
- The input \CSV data is processed to form output \CSV data.
244
+ The source \CSV data is processed to form output \CSV data.
245
+
246
+ ==== Filter \String to \String
222
247
223
- ==== Filter \String to \String Without Headers
248
+ ===== Filter \String to \String Without Headers
224
249
225
250
in_string = "foo,0\nbar,1\nbaz,2\n"
226
251
out_string = ''
@@ -230,7 +255,7 @@ The input \CSV data is processed to form output \CSV data.
230
255
end
231
256
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
232
257
233
- ==== Filter \String to \String with Headers
258
+ ===== Filter \String to \String with Headers
234
259
235
260
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
236
261
out_string = ''
@@ -240,7 +265,9 @@ The input \CSV data is processed to form output \CSV data.
240
265
end
241
266
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
242
267
243
- ==== Filter \String to \IO Stream Without Headers
268
+ ==== Filter \String to \IO Stream
269
+
270
+ ===== Filter \String to \IO Stream Without Headers
244
271
245
272
in_string = "foo,0\nbar,1\nbaz,2\n"
246
273
path = 't.csv'
@@ -252,7 +279,7 @@ The input \CSV data is processed to form output \CSV data.
252
279
end
253
280
p File.read(path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
254
281
255
- ==== Filter \String to \IO Stream with Headers
282
+ ===== Filter \String to \IO Stream with Headers
256
283
257
284
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
258
285
path = 't.csv'
@@ -264,7 +291,9 @@ The input \CSV data is processed to form output \CSV data.
264
291
end
265
292
p File.read(path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
266
293
267
- ==== Filter \IO Stream to \String Without Headers
294
+ ==== Filter \IO Stream to \String
295
+
296
+ ===== Filter \IO Stream to \String Without Headers
268
297
269
298
in_string = "foo,0\nbar,1\nbaz,2\n"
270
299
path = 't.csv'
@@ -278,7 +307,7 @@ The input \CSV data is processed to form output \CSV data.
278
307
end
279
308
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
280
309
281
- ==== Filter \IO Stream to \String with Headers
310
+ ===== Filter \IO Stream to \String with Headers
282
311
283
312
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
284
313
path = 't.csv'
@@ -292,7 +321,9 @@ The input \CSV data is processed to form output \CSV data.
292
321
end
293
322
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
294
323
295
- ==== Filter \IO Stream to \IO Stream Without Headers
324
+ ==== Filter \IO Stream to \IO Stream
325
+
326
+ ===== Filter \IO Stream to \IO Stream Without Headers
296
327
297
328
in_path = 't.csv'
298
329
in_string = "foo,0\nbar,1\nbaz,2\n"
@@ -308,7 +339,7 @@ The input \CSV data is processed to form output \CSV data.
308
339
end
309
340
p File.read(out_path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
310
341
311
- ==== Filter \IO Stream to \IO Stream with Headers
342
+ ===== Filter \IO Stream to \IO Stream with Headers
312
343
313
344
in_path = 't.csv'
314
345
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
0 commit comments