Skip to content

Commit 4534f35

Browse files
authored
Update filtering recipes to clarify parsing and writing headers (#309)
Fix GH-308
1 parent 6544b21 commit 4534f35

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

doc/csv/recipes/filtering.rdoc

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ All code snippets on this page assume that the following has been executed:
1111

1212
- {Source and Output Formats}[#label-Source+and+Output+Formats]
1313
- {Filtering String to String}[#label-Filtering+String+to+String]
14-
- {Recipe: Filter String to String with Headers}[#label-Recipe-3A+Filter+String+to+String+with+Headers]
14+
- {Recipe: Filter String to String parsing Headers}[#label-Recipe-3A+Filter+String+to+String+parsing+Headers]
15+
- {Recipe: Filter String to String parsing and writing Headers}[#label-Recipe-3A+Filter+String+to+String+parsing+and+writing+Headers]
1516
- {Recipe: Filter String to String Without Headers}[#label-Recipe-3A+Filter+String+to+String+Without+Headers]
1617
- {Filtering String to IO Stream}[#label-Filtering+String+to+IO+Stream]
17-
- {Recipe: Filter String to IO Stream with Headers}[#label-Recipe-3A+Filter+String+to+IO+Stream+with+Headers]
18+
- {Recipe: Filter String to IO Stream parsing Headers}[#label-Recipe-3A+Filter+String+to+IO+Stream+parsing+Headers]
19+
- {Recipe: Filter String to IO Stream parsing and writing Headers}[#label-Recipe-3A+Filter+String+to+IO+Stream+parsing+and+writing+Headers]
1820
- {Recipe: Filter String to IO Stream Without Headers}[#label-Recipe-3A+Filter+String+to+IO+Stream+Without+Headers]
1921
- {Filtering IO Stream to String}[#label-Filtering+IO+Stream+to+String]
20-
- {Recipe: Filter IO Stream to String with Headers}[#label-Recipe-3A+Filter+IO+Stream+to+String+with+Headers]
22+
- {Recipe: Filter IO Stream to String parsing Headers}[#label-Recipe-3A+Filter+IO+Stream+to+String+parsing+Headers]
23+
- {Recipe: Filter IO Stream to String parsing and writing Headers}[#label-Recipe-3A+Filter+IO+Stream+to+String+parsing+and+writing+Headers]
2124
- {Recipe: Filter IO Stream to String Without Headers}[#label-Recipe-3A+Filter+IO+Stream+to+String+Without+Headers]
2225
- {Filtering IO Stream to IO Stream}[#label-Filtering+IO+Stream+to+IO+Stream]
23-
- {Recipe: Filter IO Stream to IO Stream with Headers}[#label-Recipe-3A+Filter+IO+Stream+to+IO+Stream+with+Headers]
26+
- {Recipe: Filter IO Stream to IO Stream parsing Headers}[#label-Recipe-3A+Filter+IO+Stream+to+IO+Stream+parsing+Headers]
27+
- {Recipe: Filter IO Stream to IO Stream parsing and writing Headers}[#label-Recipe-3A+Filter+IO+Stream+to+IO+Stream+parsing+and+writing+Headers]
2428
- {Recipe: Filter IO Stream to IO Stream Without Headers}[#label-Recipe-3A+Filter+IO+Stream+to+IO+Stream+Without+Headers]
2529

2630
=== Source and Output Formats
@@ -33,14 +37,27 @@ The input and output \CSV data may be any mixture of \Strings and \IO streams.
3337

3438
You can filter one \String to another, with or without headers.
3539

36-
===== Recipe: Filter \String to \String with Headers
40+
===== Recipe: Filter \String to \String parsing Headers
3741

3842
Use class method CSV.filter with option +headers+ to filter a \String to another \String:
3943
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
4044
out_string = ''
4145
CSV.filter(in_string, out_string, headers: true) do |row|
42-
row[0] = row[0].upcase
43-
row[1] *= 4
46+
row['Name'] = row['Name'].upcase
47+
row['Value'] *= 4
48+
end
49+
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
50+
51+
===== Recipe: Filter \String to \String parsing and writing Headers
52+
53+
Use class method CSV.filter with option +headers+ and +out_write_headers+ to filter a \String to another \String including header row:
54+
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
55+
out_string = ''
56+
CSV.filter(in_string, out_string, headers: true, out_write_headers: true) do |row|
57+
unless row.is_a?(Array)
58+
row['Name'] = row['Name'].upcase
59+
row['Value'] *= 4
60+
end
4461
end
4562
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
4663

@@ -59,15 +76,30 @@ Use class method CSV.filter without option +headers+ to filter a \String to anot
5976

6077
You can filter a \String to an \IO stream, with or without headers.
6178

62-
===== Recipe: Filter \String to \IO Stream with Headers
79+
===== Recipe: Filter \String to \IO Stream parsing Headers
6380

6481
Use class method CSV.filter with option +headers+ to filter a \String to an \IO stream:
6582
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
6683
path = 't.csv'
6784
File.open(path, 'w') do |out_io|
6885
CSV.filter(in_string, out_io, headers: true) do |row|
69-
row[0] = row[0].upcase
70-
row[1] *= 4
86+
row['Name'] = row['Name'].upcase
87+
row['Value'] *= 4
88+
end
89+
end
90+
p File.read(path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
91+
92+
===== Recipe: Filter \String to \IO Stream parsing and writing Headers
93+
94+
Use class method CSV.filter with option +headers+ and +out_write_headers+ to filter a \String to an \IO stream including header row:
95+
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
96+
path = 't.csv'
97+
File.open(path, 'w') do |out_io|
98+
CSV.filter(in_string, out_io, headers: true, out_write_headers: true ) do |row|
99+
unless row.is_a?(Array)
100+
row['Name'] = row['Name'].upcase
101+
row['Value'] *= 4
102+
end
71103
end
72104
end
73105
p File.read(path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
@@ -89,17 +121,34 @@ Use class method CSV.filter without option +headers+ to filter a \String to an \
89121

90122
You can filter an \IO stream to a \String, with or without headers.
91123

92-
===== Recipe: Filter \IO Stream to \String with Headers
124+
===== Recipe: Filter \IO Stream to \String parsing Headers
93125

94126
Use class method CSV.filter with option +headers+ to filter an \IO stream to a \String:
95127
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
96128
path = 't.csv'
97129
File.write(path, in_string)
98130
out_string = ''
99-
File.open(path, headers: true) do |in_io|
131+
File.open(path) do |in_io|
100132
CSV.filter(in_io, out_string, headers: true) do |row|
101-
row[0] = row[0].upcase
102-
row[1] *= 4
133+
row['Name'] = row['Name'].upcase
134+
row['Value'] *= 4
135+
end
136+
end
137+
out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
138+
139+
===== Recipe: Filter \IO Stream to \String parsing and writing Headers
140+
141+
Use class method CSV.filter with option +headers+ and +out_write_headers+ to filter an \IO stream to a \String including header row:
142+
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
143+
path = 't.csv'
144+
File.write(path, in_string)
145+
out_string = ''
146+
File.open(path) do |in_io|
147+
CSV.filter(in_io, out_string, headers: true, out_write_headers: true) do |row|
148+
unless row.is_a?(Array)
149+
row['Name'] = row['Name'].upcase
150+
row['Value'] *= 4
151+
end
103152
end
104153
end
105154
out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
@@ -123,7 +172,7 @@ Use class method CSV.filter without option +headers+ to filter an \IO stream to
123172

124173
You can filter an \IO stream to another \IO stream, with or without headers.
125174

126-
===== Recipe: Filter \IO Stream to \IO Stream with Headers
175+
===== Recipe: Filter \IO Stream to \IO Stream parsing Headers
127176

128177
Use class method CSV.filter with option +headers+ to filter an \IO stream to another \IO stream:
129178
in_path = 't.csv'
@@ -133,8 +182,27 @@ Use class method CSV.filter with option +headers+ to filter an \IO stream to ano
133182
File.open(in_path) do |in_io|
134183
File.open(out_path, 'w') do |out_io|
135184
CSV.filter(in_io, out_io, headers: true) do |row|
136-
row[0] = row[0].upcase
137-
row[1] *= 4
185+
row['Name'] = row['Name'].upcase
186+
row['Value'] *= 4
187+
end
188+
end
189+
end
190+
p File.read(out_path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
191+
192+
===== Recipe: Filter \IO Stream to \IO Stream parsing and writing Headers
193+
194+
Use class method CSV.filter with option +headers+ and +out_write_headers+ to filter an \IO stream to another \IO stream including header row:
195+
in_path = 't.csv'
196+
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
197+
File.write(in_path, in_string)
198+
out_path = 'u.csv'
199+
File.open(in_path) do |in_io|
200+
File.open(out_path, 'w') do |out_io|
201+
CSV.filter(in_io, out_io, headers: true, out_write_headers: true) do |row|
202+
unless row.is_a?(Array)
203+
row['Name'] = row['Name'].upcase
204+
row['Value'] *= 4
205+
end
138206
end
139207
end
140208
end

0 commit comments

Comments
 (0)