Skip to content

Commit aea896f

Browse files
Recipes for field converters (#177)
1 parent 338b7f0 commit aea896f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doc/csv/recipes.rdoc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
== Recipes
22

3+
All code snippets on this page assume that the following has been executed:
4+
require 'csv'
5+
36
=== Contents
47

58
- {Parsing: Source Formats}[#label-Parsing-3A+Source+Formats]
@@ -12,6 +15,10 @@
1215
- {Parse from IO Stream}[#label-Parse+from+IO+Stream]
1316
- {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers]
1417
- {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]
1522
- {Generating: Output Formats}[#label-Generating-3A+Output+Formats]
1623
- {Generate to String}[#label-Generate+to+String]
1724
- {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers]
@@ -152,6 +159,52 @@ Output:
152159
#<CSV::Row "Name":"bar" "Value":"1">
153160
#<CSV::Row "Name":"baz" "Value":"2">
154161

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+
155208
=== Generating: Output Formats
156209

157210
==== Generate to \String Without Headers

0 commit comments

Comments
 (0)