@@ -46,16 +46,20 @@ All code snippets on this page assume that the following has been executed:
46
46
47
47
=== Parsing: Source Formats
48
48
49
+ You can parse \CSV data from a \String, from a \File (via its path), or from an \IO stream.
50
+
49
51
==== Parse from \String
50
52
53
+ You can parse \CSV data from a \String, with or without headers.
54
+
51
55
===== Parse from \String with Headers
52
56
53
- \Class method CSV.parse can read a source \String all at once,
54
- and so may have memory resource implications:
57
+ Use class method CSV.parse with option +headers+ to read a source \String all at once
58
+ ( may have memory resource implications) :
55
59
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
56
60
CSV.parse(string, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
57
61
58
- Instance method CSV#each can read a source \String one row at a time:
62
+ Use instance method CSV#each with option +headers+ to read a source \String one row at a time:
59
63
CSV.new(string, headers: true).each do |row|
60
64
p row
61
65
end
@@ -66,12 +70,12 @@ Ouput:
66
70
67
71
===== Parse from \String Without Headers
68
72
69
- \Class method CSV.parse can read a source \String all at once,
70
- and so may have memory resource implications:
73
+ Use class method CSV.parse without option +headers+ to read a source \String all at once
74
+ ( may have memory resource implications) :
71
75
string = "foo,0\nbar,1\nbaz,2\n"
72
76
CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
73
77
74
- Instance method CSV#each can read a source \String one row at a time:
78
+ Use instance method CSV#each without option +headers+ to read a source \String one row at a time:
75
79
CSV.new(string).each do |row|
76
80
p row
77
81
end
@@ -82,15 +86,17 @@ Output:
82
86
83
87
==== Parse from \File
84
88
89
+ You can parse \CSV data from a \File, with or without headers.
90
+
85
91
===== Parse from \File with Headers
86
92
87
- Instance method CSV#read can reada file all at once:
93
+ Use instance method CSV#read with option +headers+ to read a file all at once:
88
94
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
89
95
path = 't.csv'
90
96
File.write(path, string)
91
97
CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
92
98
93
- \Class method CSV.foreach can read one row at a time:
99
+ Use class method CSV.foreach with option +headers+ to read one row at a time:
94
100
CSV.foreach(path, headers: true) do |row|
95
101
p row
96
102
end
@@ -101,13 +107,13 @@ Output:
101
107
102
108
===== Parse from \File Without Headers
103
109
104
- \Class method CSV.read can read a file all at once:
110
+ Use class method CSV.read without option +headers+ to read a file all at once:
105
111
string = "foo,0\nbar,1\nbaz,2\n"
106
112
path = 't.csv'
107
113
File.write(path, string)
108
114
CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
109
115
110
- \Class method CSV.foreach can read one row at a time:
116
+ Use class method CSV.foreach without option +headers+ to read one row at a time:
111
117
CSV.foreach(path) do |row|
112
118
p row
113
119
end
@@ -118,17 +124,19 @@ Output:
118
124
119
125
==== Parse from \IO Stream
120
126
127
+ You can parse \CSV data from an \IO stream, with or without headers.
128
+
121
129
===== Parse from \IO Stream with Headers
122
130
123
- \Class method CSV.parse can read an \IO stream all at once:
131
+ Use class method CSV.parse with option +headers+ to read an \IO stream all at once:
124
132
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
125
133
path = 't.csv'
126
134
File.write(path, string)
127
135
File.open(path) do |file|
128
136
CSV.parse(file, headers: true)
129
137
end # => #<CSV::Table mode:col_or_row row_count:4>
130
138
131
- \Class method CSV.foreach can read one row at a time:
139
+ Use class method CSV.foreach with option +headers+ to read one row at a time:
132
140
File.open(path) do |file|
133
141
CSV.foreach(file, headers: true) do |row|
134
142
p row
@@ -141,15 +149,15 @@ Output:
141
149
142
150
===== Parse from \IO Stream Without Headers
143
151
144
- \Class method CSV.parse can read an \IO stream all at once:
152
+ Use class method CSV.parse without option +headers+ to read an \IO stream all at once:
145
153
string = "foo,0\nbar,1\nbaz,2\n"
146
154
path = 't.csv'
147
155
File.write(path, string)
148
156
File.open(path) do |file|
149
157
CSV.parse(file)
150
158
end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
151
159
152
- \Class method CSV.foreach can read one row at a time:
160
+ Use class method CSV.foreach without option +headers+ to read one row at a time:
153
161
File.open(path) do |file|
154
162
CSV.foreach(file) do |row|
155
163
p row
@@ -162,9 +170,12 @@ Output:
162
170
163
171
=== Parsing: Field Converters
164
172
173
+ You can use field converters to change parsed Strings into other objects,
174
+ or to otherwise modify \String fields.
175
+
165
176
==== Convert Fields to Objects
166
177
167
- Use field converters to change parsed Strings into other, more specific, object .
178
+ Use field converters to change parsed Strings into other, more specific, objects .
168
179
169
180
==== Convert Fields to Objects Using Built-In Converters
170
181
@@ -189,6 +200,7 @@ Output:
189
200
190
201
==== Convert Fields to Objects Using Custom Converters
191
202
203
+ You can define custom field converters to convert \String fields into other objects.
192
204
This example defines and uses a custom field converter
193
205
that converts each column-1 value to a \Rational object.
194
206
@@ -213,6 +225,7 @@ You can also register a custom field converter, then refer to it by name:
213
225
214
226
==== Filter Field Strings
215
227
228
+ You can define custom field converters to modify \String fields.
216
229
This example defines and uses a custom field converter
217
230
that strips whitespace from each field value.
218
231
@@ -235,11 +248,15 @@ You can also register a custom field converter, then refer to it by name:
235
248
236
F438
249
=== Generating: Output Formats
237
250
251
+ You can generate \CSV output to a \String, to a \File (via its path), or to an \IO stream.
252
+
238
253
==== Generate to \String
239
254
255
+ You can generate \CSV output to a \String, with or without headers.
256
+
240
257
===== Generate to \String with Headers
241
258
242
- \Class method CSV.generate can generate to a \String.
259
+ Use class method CSV.generate with option +headers+ to generate to a \String.
243
260
244
261
This example uses method CSV#<< to append the rows
245
262
that are to be generated:
@@ -252,7 +269,7 @@ that are to be generated:
252
269
253
270
===== Generate to \String Without Headers
254
271
255
- \Class method CSV.generate can generate to a \String.
272
+ Use class method CSV.generate without option +headers+ to generate to a \String.
256
273
257
274
This example uses method CSV#<< to append the rows
258
275
that are to be generated:
@@ -265,9 +282,11 @@ that are to be generated:
265
282
266
283
==== Generate to \File
267
284
285
+ You can generate /CSV data to a \File, with or without headers.
286
+
268
287
===== Generate to \File with Headers
269
288
270
- \Class method CSV.open can generate to a \File.
289
+ Use class method CSV.open with option +headers+ generate to a \File.
271
290
272
291
This example uses method CSV#<< to append the rows
273
292
that are to be generated:
@@ -281,7 +300,7 @@ that are to be generated:
281
300
282
301
===== Generate to \File Without Headers
283
302
284
- \Class method CSV.open can generate to a \File.
303
+ Use class method CSV.open without option +headers+ to generate to a \File.
285
304
286
305
This example uses method CSV#<< to append the rows
287
306
that are to be generated:
@@ -295,9 +314,11 @@ that are to be generated:
295
314
296
315
==== Generate to \IO Stream
297
316
317
+ You can generate \CSV data to an \IO stream, with or without headers.
318
+
298
319
==== Generate to \IO Stream with Headers
299
320
300
- \\Classs method CSV.new can generate \CSV data to an \IO stream:
321
+ Use class method CSV.new with option +headers+ to generate \CSV data to an \IO stream:
301
322
path = 't.csv'
302
323
File.open(path, 'w') do |file|
303
324
csv = CSV.new(file, headers: ['Name', 'Value'], write_headers: true)
@@ -309,7 +330,7 @@ that are to be generated:
309
330
310
331
===== Generate to \IO Stream Without Headers
311
332
312
- \Class method CSV.new can generate \CSV data to an \IO stream:
333
+ Use class method CSV.new without option +headers+ to generate \CSV data to an \IO stream:
313
334
path = 't.csv'
314
335
File.open(path, 'w') do |file|
315
336
csv = CSV.new(file)
@@ -321,13 +342,17 @@ that are to be generated:
321
342
322
343
=== Filtering: Source and Output Formats
323
344
324
- \Class method CSV.filter provides a Unix-style filter for \CSV data.
325
- The source \CSV data is processed to form output \CSV data.
345
+ You can use a Unix-style "filter" for \CSV data.
346
+ The filter reads source \CSV data and writes output \CSV data as modified by the filter.
347
+ The input and output \CSV data may be any mixture of \Strings and \IO streams.
326
348
327
349
==== Filter \String to \String
328
350
351
+ You can filter one \String to another, with or without headers.
352
+
329
353
===== Filter \String to \String with Headers
330
354
355
+ Use class method CSV.filter with option +headers+ to filter a \String to another \String:
331
356
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
332
357
out_string = ''
333
358
CSV.filter(in_string, out_string, headers: true) do |row|
@@ -338,6 +363,7 @@ The source \CSV data is processed to form output \CSV data.
338
363
339
364
===== Filter \String to \String Without Headers
340
365
366
+ Use class method CSV.filter without option +headers+ to filter a \String to another \String:
341
367
in_string = "foo,0\nbar,1\nbaz,2\n"
342
368
out_string = ''
343
369
CSV.filter(in_string, out_string) do |row|
@@ -348,8 +374,11 @@ The source \CSV data is processed to form output \CSV data.
348
374
349
375
==== Filter \String to \IO Stream
350
376
377
+ You can filter a \String to an \IO stream, with or without headers.
378
+
351
379
===== Filter \String to \IO Stream with Headers
352
380
381
+ Use class method CSV.filter with option +headers+ to filter a \String to an \IO stream:
353
382
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
354
383
path = 't.csv'
355
384
File.open(path, 'w') do |out_io|
@@ -362,6 +391,7 @@ The source \CSV data is processed to form output \CSV data.
362
391
363
392
===== Filter \String to \IO Stream Without Headers
364
393
394
+ Use class method CSV.filter without option +headers+ to filter a \String to an \IO stream:
365
395
in_string = "foo,0\nbar,1\nbaz,2\n"
366
396
path = 't.csv'
367
397
File.open(path, 'w') do |out_io|
@@ -374,8 +404,11 @@ The source \CSV data is processed to form output \CSV data.
374
404
375
405
==== Filter \IO Stream to \String
376
406
407
+ You can filter an \IO stream to a \String, with or without headers.
408
+
377
409
===== Filter \IO Stream to \String with Headers
378
410
411
+ Use class method CSV.filter with option +headers+ to filter an \IO stream to a \String:
379
412
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
380
413
path = 't.csv'
381
414
File.write(path, in_string)
@@ -390,6 +423,7 @@ The source \CSV data is processed to form output \CSV data.
390
423
391
424
===== Filter \IO Stream to \String Without Headers
392
425
426
+ Use class method CSV.filter without option +headers+ to filter an \IO stream to a \String:
393
427
in_string = "foo,0\nbar,1\nbaz,2\n"
394
428
path = 't.csv'
395
429
File.write(path, in_string)
@@ -404,8 +438,11 @@ The source \CSV data is processed to form output \CSV data.
404
438
405
439
==== Filter \IO Stream to \IO Stream
406
440
441
+ You can filter an \IO stream to another \IO stream, with or without headers.
442
+
407
443
===== Filter \IO Stream to \IO Stream with Headers
408
444
445
+ Use class method CSV.filter with option +headers+ to filter an \IO stream to another \IO stream:
409
446
in_path = 't.csv'
410
447
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
411
448
File.write(in_path, in_string)
@@ -422,6 +459,7 @@ The source \CSV data is processed to form output \CSV data.
422
459
423
460
===== Filter \IO Stream to \IO Stream Without Headers
424
461
462
+ Use class method CSV.filter without option +headers+ to filter an \IO stream to another \IO stream:
425
463
in_path = 't.csv'
426
464
in_string = "foo,0\nbar,1\nbaz,2\n"
427
465
File.write(in_path, in_string)
0 commit comments