|
1 | 1 | == Recipes |
2 | 2 |
|
| 3 | +All code snippets on this page assume that the following has been executed: |
| 4 | + require 'csv' |
| 5 | + |
3 | 6 | === Contents |
4 | 7 |
|
5 | 8 | - {Parsing: Source Formats}[#label-Parsing-3A+Source+Formats] |
|
12 | 15 | - {Parse from IO Stream}[#label-Parse+from+IO+Stream] |
13 | 16 | - {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers] |
14 | 17 | - {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers] |
| 18 | +- {Parsing: Field Converters}[#label-Parsing-3A+Field+Converters] |
| 19 | + - {Convert Fields to Objects}[#label-Convert+Fields+to+Objects] |
| 20 | + - {Convert Fields to Objects Using Built-In Converters}[#label-Convert+Fields+to+Objects+Using+Built-In+Converters] |
| 21 | + - {Convert Fields to Objects Using Custom Converters}[#label-Convert+Fields+to+Objects+Using+Custom+Converters] |
15 | 22 | - {Generating: Output Formats}[#label-Generating-3A+Output+Formats] |
16 | 23 | - {Generate to String}[#label-Generate+to+String] |
17 | 24 | - {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers] |
@@ -152,6 +159,52 @@ Output: |
152 | 159 | #<CSV::Row "Name":"bar" "Value":"1"> |
153 | 160 | #<CSV::Row "Name":"baz" "Value":"2"> |
154 | 161 |
|
| 162 | +=== Parsing: Field Converters |
| 163 | + |
| 164 | +==== Convert Fields to Objects |
| 165 | + |
| 166 | +Use field converters to change parsed Strings into other, more specific, object. |
| 167 | + |
| 168 | +==== Convert Fields to Objects Using Built-In Converters |
| 169 | + |
| 170 | +Without converters (all fields parsed as Strings): |
| 171 | + source = "0,1.1,2020-09-19" |
| 172 | + parsed = CSV.parse(source) |
| 173 | + parsed # => [["0", "1.1", "2020-09-19"]] |
| 174 | + parsed.first.each {|field| p field.class } |
| 175 | +Output: |
| 176 | + String |
| 177 | + String |
| 178 | + String |
| 179 | + |
| 180 | +With built-in converters (see {Built-In Field Converters}[../../CSV.html#class-CSV-label-Built-In+Field+Converters]): |
| 181 | + parsed = CSV.parse(source, converters: :all) |
| 182 | + parsed # => [[0, 1.1, #<DateTime: 2020-09-19T00:00:00+00:00 ((2459112j,0s,0n),+0s,2299161j)>]] |
| 183 | + parsed.first.each {|field| p field.class } |
| 184 | +Output: |
| 185 | + Integer |
| 186 | + Float |
| 187 | + DateTime |
| 188 | + |
| 189 | +==== Convert Fields to Objects Using Custom Converters |
| 190 | + |
| 191 | +Define a custom field converter: |
| 192 | + strip_converter = proc {|field| field.strip } |
| 193 | + |
| 194 | +Without the new converter: |
| 195 | + string = " foo , 0 \n bar , 1 \n baz , 2 \n" |
| 196 | + array = CSV.parse(string) |
| 197 | + array # => [[" foo ", " 0 "], [" bar ", " 1 "], [" baz ", " 2 "]] |
| 198 | + |
| 199 | +With the new converter: |
| 200 | + array = CSV.parse(string, converters: strip_converter) |
| 201 | + array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
| 202 | + |
| 203 | +You can also register a custom field converter, then refer to it by name: |
| 204 | + CSV::Converters[:strip] = strip_converter |
| 205 | + array = CSV.parse(string, converters: :strip) |
| 206 | + array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
| 207 | + |
155 | 208 | === Generating: Output Formats |
156 | 209 |
|
157 | 210 | ==== Generate to \String Without Headers |
|
0 commit comments