@@ -17,9 +17,18 @@ All code snippets on this page assume that the following has been executed:
1717 - {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
1818- {Parsing: Field Converters}[#label-Parsing-3A+Field+Converters]
1919 - {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]
20+ - {Convert Fields to Integers}[#label-Convert+Fields+to+Integers]
21+ - {Convert Fields to Floats}[#label-Convert+Fields+to+Floats]
22+ - {Convert Fields to Numerics}[#label-Convert+Fields+to+Numerics]
23+ - {Convert Fields to Dates}[#label-Convert+Fields+to+Dates]
24+ - {Convert Fields to DateTimes}[#label-Convert+Fields+to+DateTimes]
25+ - {Convert Assorted Fields to Objects}[#label-Convert+Assorted+Fields+to+Objects]
26+ - {Convert Fields to Other Objects}[#label-Convert+Fields+to+Other+Objects]
2227 - {Filter Field Strings}[#label-Filter+Field+Strings]
28+ - {Register Field Converters}[#label-Register+Field+Converters]
29+ - {Use Multiple Field Converters}[#label-Use+Multiple+Field+Converters]
30+ - {Specify Multiple Field Converters in Option :converters}[#label-Specify+Multiple+Field+Converters+in+Option+-3Aconverters]
31+ - {Specify Multiple Field Converters in a Custom Converter List}[#label-Specify+Multiple+Field+Converters+in+a+Custom+Converter+List]
2332- {Generating: Output Formats}[#label-Generating-3A+Output+Formats]
2433 - {Generate to String}[#label-Generate+to+String]
2534 - {Generate to String with Headers}[#label-Generate+to+String+with+Headers]
@@ -170,81 +179,122 @@ Output:
170179
171180=== Parsing: Field Converters
172181
173- You can use field converters to change parsed Strings into other objects,
174- or to otherwise modify \String fields.
182+ You can use field converters to change parsed \String fields into other objects,
183+ or to otherwise modify the \String fields.
175184
176185==== Convert Fields to Objects
177186
178- Use field converters to change parsed Strings into other, more specific, objects.
187+ Use field converters to change parsed \String objects into other, more specific, objects.
179188
180- ==== Convert Fields to Objects Using Built-In Converters
189+ There are built-in field converters for converting to objects of certain classes:
190+ - \Float
191+ - \Integer
192+ - \Date
193+ - \DateTime
181194
182- Without converters (all fields parsed as Strings):
183- source = "0,1.1,2020-09-19"
184- parsed = CSV.parse(source)
185- parsed # => [["0", "1.1", "2020-09-19"]]
186- parsed.first.each {|field| p field.class }
187- Output:
188- String
189- String
190- String
191-
192- With built-in converters (see {Built-In Field Converters}[../../CSV.html#class-CSV-label-Built-In+Field+Converters]):
193- parsed = CSV.parse(source, converters: :all)
194- parsed # => [[0, 1.1, #<DateTime: 2020-09-19T00:00:00+00:00 ((2459112j,0s,0n),+0s,2299161j)>]]
195- parsed.first.each {|field| p field.class }
196- Output:
197- Integer
198- Float
199- DateTime
195+ Other built-in field converters include:
196+ - <tt>:numeric</tt>: converts to \Integer and \Float.
197+ - <tt>:all</tt>: converts to \DateTime, \Integer, \Float.
200198
201- ==== Convert Fields to Objects Using Custom Converters
199+ You can also define field converters to convert to objects of other classes.
202200
203- You can define custom field converters to convert \String fields into other objects.
204- This example defines and uses a custom field converter
205- that converts each column-1 value to a \Rational object.
201+ ===== Convert Fields to Integers
202+
203+ Convert fields to \Integer objects using built-in converter <tt>:integer</tt>:
204+ source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
205+ parsed = CSV.parse(source, headers: true, converters: :integer)
206+ parsed.map {|row| row['Value'].class} # => [Integer, Integer, Integer]
207+
208+ ===== Convert Fields to Floats
209+
210+ Convert fields to \Float objects using built-in converter <tt>:float</tt>:
211+ source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
212+ parsed = CSV.parse(source, headers: true, converters: :float)
213+ parsed.map {|row| row['Value'].class} # => [Float, Float, Float]
214+
215+ ===== Convert Fields to Numerics
216+
217+ Convert fields to \Integer and \Float objects using built-in converter <tt>:numeric</tt>:
218+ source = "Name,Value\nfoo,0\nbar,1.1\nbaz,2.2\n"
219+ parsed = CSV.parse(source, headers: true, converters: :numeric)
220+ parsed.map {|row| row['Value'].class} # => [Integer, Float, Float]
221+
222+ ===== Convert Fields to Dates
223+
224+ Convert fields to \Date objects using built-in converter <tt>:date</tt>:
225+ source = "Name,Date\nfoo,2001-02-03\nbar,2001-02-04\nbaz,2001-02-03\n"
226+ parsed = CSV.parse(source, headers: true, converters: :date)
227+ parsed.map {|row| row['Date'].class} # => [Date, Date, Date]
228+
229+ ===== Convert Fields to DateTimes
230+
231+ Convert fields to \DateTime objects using built-in converter <tt>:date_time</tt>:
232+ source = "Name,DateTime\nfoo,2001-02-03\nbar,2001-02-04\nbaz,2020-05-07T14:59:00-05:00\n"
233+ parsed = CSV.parse(source, headers: true, converters: :date_time)
234+ parsed.map {|row| row['DateTime'].class} # => [DateTime, DateTime, DateTime]
235+
236+ ===== Convert Assorted Fields to Objects
206237
207- Define a custom field converter:
238+ Convert assorted fields to objects using built-in converter <tt>:all</tt>:
239+ source = "Type,Value\nInteger,0\nFloat,1.0\nDateTime,2001-02-04\n"
240+ parsed = CSV.parse(source, headers: true, converters: :all)
241+ parsed.map {|row| row['Value'].class} # => [Integer, Float, DateTime]
242+
243+ ===== Convert Fields to Other Objects
244+
245+ Define a custom field converter to convert \String fields into other objects.
246+ This example defines and uses a custom field converter
247+ that converts each column-1 value to a \Rational object:
208248 rational_converter = proc do |field, field_context|
209249 field_context.index == 1 ? field.to_r : field
210250 end
251+ source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
252+ parsed = CSV.parse(source, headers: true, converters: rational_converter)
253+ parsed.map {|row| row['Value'].class} # => [Rational, Rational, Rational]
211254
212- Without the new converter:
213- string = "foo,0\nbar,1\nbaz,2\n"
214- array = CSV.parse(string)
215- array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
255+ ==== Filter Field Strings
256+
257+ Define a custom field converter to modify \String fields.
258+ This example defines and uses a custom field converter
259+ that strips whitespace from each field value:
260+ strip_converter = proc {|field| field.strip }
261+ source = "Name,Value\n foo , 0 \n bar , 1 \n baz , 2 \n"
262+ parsed = CSV.parse(source, headers: true, converters: strip_converter)
263+ parsed['Name'] # => ["foo", "bar", "baz"]
264+ parsed['Value'] # => ["0", "1", "2"]
216265
217- With the new converter:
218- array = CSV.parse(string, converters: rational_converter)
219- array # => [["foo", (0/1)], ["bar", (1/1)], ["baz", (2/1)]]
266+ ==== Register Field Converters
220267
221- You can also register a custom field converter, then refer to it by name:
268+ Register a custom field converter, assigning it a name;
269+ then refer to the converter by its name:
222270 CSV::Converters[:rational] = rational_converter
223- array = CSV.parse(string, converters: :rational)
224- array # => [["foo", (0/1)], ["bar", (1/1)], ["baz", (2/1)]]
271+ source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
272+ parsed = CSV.parse(source, headers: true, converters: :rational)
273+ parsed['Value'] # => [(0/1), (1/1), (2/1)]
225274
226- ==== Filter Field Strings
275+ ==== Use Multiple Field Converters
227276
228- You can define custom field converters to modify \String fields.
229- This example defines and uses a custom field converter
230- that strips whitespace from each field value .
277+ You can use multiple field converters in either of these ways:
278+ - Specify converters in option <tt>:converters</tt>.
279+ - Specify converters in a custom converter list .
231280
232- Define a custom field converter:
233- strip_converter = proc {|field| field.strip }
281+ ===== Specify Multiple Field Converters in Option <tt>:converters</tt>
234282
235- Without the new converter :
236- string = " foo , 0 \n bar , 1 \n baz , 2 \n"
237- array = CSV.parse(string )
238- array # => [[" foo ", " 0 "], [" bar ", " 1 "], [" baz ", " 2 "] ]
283+ Apply multiple field converters by specifying them in option <tt>:conveters</tt> :
284+ source = "Name,Value\nfoo,0\nbar,1.0\nbaz,2.0 \n"
285+ parsed = CSV.parse(source, headers: true, converters: [:integer, :float] )
286+ parsed['Value'] # => [0, 1.0, 2.0 ]
239287
240- With the new converter:
241- array = CSV.parse(string, converters: strip_converter)
242- array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
288+ ===== Specify Multiple Field Converters in a Custom Converter List
243289
244- You can also register a custom field converter, then refer to it by name:
290+ Apply multiple field converters by defining and registering a custom converter list:
291+ strip_converter = proc {|field| field.strip }
245292 CSV::Converters[:strip] = strip_converter
246- array = CSV.parse(string, converters: :strip)
247- array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
293+ CSV::Converters[:my_converters] = [:integer, :float, :strip]
294+ source = "Name,Value\n foo , 0 \n bar , 1.0 \n baz , 2.0 \n"
295+ parsed = CSV.parse(source, headers: true, converters: :my_converters)
296+ parsed['Name'] # => ["foo", "bar", "baz"]
297+ parsed['Value'] # => [0, 1.0, 2.0]
248298
249299=== Generating: Output Formats
250300
0 commit comments