@@ -46,16 +46,20 @@ All code snippets on this page assume that the following has been executed:
4646
4747=== Parsing: Source Formats
4848
49+ You can parse \CSV data from a \String, from a \File (via its path), or from an \IO stream.
50+
4951==== Parse from \String
5052
53+ You can parse \CSV data from a \String, with or without headers.
54+
5155===== Parse from \String with Headers
5256
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) :
5559 string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
5660 CSV.parse(string, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
5761
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:
5963 CSV.new(string, headers: true).each do |row|
6064 p row
6165 end
@@ -66,12 +70,12 @@ Ouput:
6670
6771===== Parse from \String Without Headers
6872
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) :
7175 string = "foo,0\nbar,1\nbaz,2\n"
7276 CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
7377
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:
7579 CSV.new(string).each do |row|
7680 p row
7781 end
@@ -82,15 +86,17 @@ Output:
8286
8387==== Parse from \File
8488
89+ You can parse \CSV data from a \File, with or without headers.
90+
8591===== Parse from \File with Headers
8692
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:
8894 string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
8995 path = 't.csv'
9096 File.write(path, string)
9197 CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
9298
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:
94100 CSV.foreach(path, headers: true) do |row|
95101 p row
96102 end
@@ -101,13 +107,13 @@ Output:
101107
102108===== Parse from \File Without Headers
103109
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:
105111 string = "foo,0\nbar,1\nbaz,2\n"
106112 path = 't.csv'
107113 File.write(path, string)
108114 CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
109115
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:
111117 CSV.foreach(path) do |row|
112118 p row
113119 end
@@ -118,17 +124,19 @@ Output:
118124
119125==== Parse from \IO Stream
120126
127+ You can parse \CSV data from an \IO stream, with or without headers.
128+
121129===== Parse from \IO Stream with Headers
122130
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:
124132 string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
125133 path = 't.csv'
126134 File.write(path, string)
127135 File.open(path) do |file|
128136 CSV.parse(file, headers: true)
129137 end # => #<CSV::Table mode:col_or_row row_count:4>
130138
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:
132140 File.open(path) do |file|
133141 CSV.foreach(file, headers: true) do |row|
134142 p row
@@ -141,15 +149,15 @@ Output:
141149
142150===== Parse from \IO Stream Without Headers
143151
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:
145153 string = "foo,0\nbar,1\nbaz,2\n"
146154 path = 't.csv'
147155 File.write(path, string)
148156 File.open(path) do |file|
149157 CSV.parse(file)
150158 end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
151159
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:
153161 File.open(path) do |file|
154162 CSV.foreach(file) do |row|
155163 p row
@@ -162,9 +170,12 @@ Output:
162170
163171=== Parsing: Field Converters
164172
173+ You can use field converters to change parsed Strings into other objects,
174+ or to otherwise modify \String fields.
175+
165176==== Convert Fields to Objects
166177
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 .
168179
169180==== Convert Fields to Objects Using Built-In Converters
170181
@@ -189,6 +200,7 @@ Output:
189200
190201==== Convert Fields to Objects Using Custom Converters
191202
203+ You can define custom field converters to convert \String fields into other objects.
192204This example defines and uses a custom field converter
193205that converts each column-1 value to a \Rational object.
194206
@@ -213,6 +225,7 @@ You can also register a custom field converter, then refer to it by name:
213225
214226==== Filter Field Strings
215227
228+ You can define custom field converters to modify \String fields.
216229This example defines and uses a custom field converter
217230that strips whitespace from each field value.
218231
@@ -235,11 +248,15 @@ You can also register a custom field converter, then refer to it by name:
235248
236249=== Generating: Output Formats
237250
251+ You can generate \CSV output to a \String, to a \File (via its path), or to an \IO stream.
252+
238253==== Generate to \String
239254
255+ You can generate \CSV output to a \String, with or without headers.
256+
240257===== Generate to \String with Headers
241258
242- \Class method CSV.generate can generate to a \String.
259+ Use class method CSV.generate with option +headers+ to generate to a \String.
243260
244261This example uses method CSV#<< to append the rows
245262that are to be generated:
@@ -252,7 +269,7 @@ that are to be generated:
252269
253270===== Generate to \String Without Headers
254271
255- \Class method CSV.generate can generate to a \String.
272+ Use class method CSV.generate without option +headers+ to generate to a \String.
256273
257274This example uses method CSV#<< to append the rows
258275that are to be generated:
@@ -265,9 +282,11 @@ that are to be generated:
265282
266283==== Generate to \File
267284
285+ You can generate /CSV data to a \File, with or without headers.
286+
268287===== Generate to \File with Headers
269288
270- \Class method CSV.open can generate to a \File.
289+ Use class method CSV.open with option +headers+ generate to a \File.
271290
272291This example uses method CSV#<< to append the rows
273292that are to be generated:
@@ -281,7 +300,7 @@ that are to be generated:
281300
282301===== Generate to \File Without Headers
283302
284- \Class method CSV.open can generate to a \File.
303+ Use class method CSV.open without option +headers+ to generate to a \File.
285304
286305This example uses method CSV#<< to append the rows
287306that are to be generated:
@@ -295,9 +314,11 @@ that are to be generated:
295314
296315==== Generate to \IO Stream
297316
317+ You can generate \CSV data to an \IO stream, with or without headers.
318+
298319==== Generate to \IO Stream with Headers
299320
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:
301322 path = 't.csv'
302323 File.open(path, 'w') do |file|
303324 csv = CSV.new(file, headers: ['Name', 'Value'], write_headers: true)
@@ -309,7 +330,7 @@ that are to be generated:
309330
310331===== Generate to \IO Stream Without Headers
311332
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:
313334 path = 't.csv'
314335 File.open(path, 'w') do |file|
315336 csv = CSV.new(file)
@@ -321,13 +342,17 @@ that are to be generated:
321342
322343=== Filtering: Source and Output Formats
323344
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.
326348
327349==== Filter \String to \String
328350
351+ You can filter one \String to another, with or without headers.
352+
329353===== Filter \String to \String with Headers
330354
355+ Use class method CSV.filter with option +headers+ to filter a \String to another \String:
331356 in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
332357 out_string = ''
333358 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.
338363
339364===== Filter \String to \String Without Headers
340365
366+ Use class method CSV.filter without option +headers+ to filter a \String to another \String:
341367 in_string = "foo,0\nbar,1\nbaz,2\n"
342368 out_string = ''
343369 CSV.filter(in_string, out_string) do |row|
@@ -348,8 +374,11 @@ The source \CSV data is processed to form output \CSV data.
348374
349375==== Filter \String to \IO Stream
350376
377+ You can filter a \String to an \IO stream, with or without headers.
378+
351379===== Filter \String to \IO Stream with Headers
352380
381+ Use class method CSV.filter with option +headers+ to filter a \String to an \IO stream:
353382 in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
354383 path = 't.csv'
355384 File.open(path, 'w') do |out_io|
@@ -362,6 +391,7 @@ The source \CSV data is processed to form output \CSV data.
362391
363392===== Filter \String to \IO Stream Without Headers
364393
394+ Use class method CSV.filter without option +headers+ to filter a \String to an \IO stream:
365395 in_string = "foo,0\nbar,1\nbaz,2\n"
366396 path = 't.csv'
367397 File.open(path, 'w') do |out_io|
@@ -374,8 +404,11 @@ The source \CSV data is processed to form output \CSV data.
374404
375405==== Filter \IO Stream to \String
376406
407+ You can filter an \IO stream to a \String, with or without headers.
408+
377409===== Filter \IO Stream to \String with Headers
378410
411+ Use class method CSV.filter with option +headers+ to filter an \IO stream to a \String:
379412 in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
380413 path = 't.csv'
381414 File.write(path, in_string)
@@ -390,6 +423,7 @@ The source \CSV data is processed to form output \CSV data.
390423
391424===== Filter \IO Stream to \String Without Headers
392425
426+ Use class method CSV.filter without option +headers+ to filter an \IO stream to a \String:
393427 in_string = "foo,0\nbar,1\nbaz,2\n"
394428 path = 't.csv'
395429 File.write(path, in_string)
@@ -404,8 +438,11 @@ The source \CSV data is processed to form output \CSV data.
404438
405439==== Filter \IO Stream to \IO Stream
406440
441+ You can filter an \IO stream to another \IO stream, with or without headers.
442+
407443===== Filter \IO Stream to \IO Stream with Headers
408444
445+ Use class method CSV.filter with option +headers+ to filter an \IO stream to another \IO stream:
409446 in_path = 't.csv'
410447 in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
411448 File.write(in_path, in_string)
@@ -422,6 +459,7 @@ The source \CSV data is processed to form output \CSV data.
422459
423460===== Filter \IO Stream to \IO Stream Without Headers
424461
462+ Use class method CSV.filter without option +headers+ to filter an \IO stream to another \IO stream:
425463 in_path = 't.csv'
426464 in_string = "foo,0\nbar,1\nbaz,2\n"
427465 File.write(in_path, in_string)
0 commit comments