@@ -58,6 +58,9 @@ All code snippets on this page assume that the following has been executed:
58
58
- {Using Multiple Header Converters}[#label-Using+Multiple+Header+Converters]
59
59
- {Recipe: Specify Multiple Header Converters in Option :header_converters}[#label-Recipe-3A+Specify+Multiple+Header+Converters+in+Option+-3Aheader_converters]
60
60
- {Recipe: Specify Multiple Header Converters in a Custom Header Converter List}[#label-Recipe-3A+Specify+Multiple+Header+Converters+in+a+Custom+Header+Converter+List]
61
+ - {Diagnostics}[#label-Diagnostics]
62
+ - {Recipe: Capture Unconverted Fields}[#label-Recipe-3A+Capture+Unconverted+Fields]
63
+ - {Recipe: Capture Field Info}[#label-Recipe-3A+Capture+Field+Info]
61
64
62
65
=== Source Formats
63
66
@@ -507,3 +510,34 @@ Apply multiple header converters by defining and registering a custom header con
507
510
source = "NAME,VALUE\nfoo,0\nbar,1.0\nbaz,2.0\n"
508
511
parsed = CSV.parse(source, headers: true, header_converters: :my_header_converters)
509
512
parsed.headers # => [:name, :value]
513
+
514
+ === Diagnostics
515
+
516
+ ==== Recipe: Capture Unconverted Fields
517
+
518
+ To capture unconverted field values, use option +:unconverted_fields+:
519
+ source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
520
+ parsed = CSV.parse(source, converters: :integer, unconverted_fields: true)
521
+ parsed # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
522
+ parsed.each {|row| p row.unconverted_fields }
523
+ Output:
524
+ ["Name", "Value"]
525
+ ["foo", "0"]
526
+ ["bar", "1"]
527
+ ["baz", "2"]
528
+
529
+ ==== Recipe: Capture Field Info
530
+
531
+ To capture field info in a custom converter, accept two block arguments.
532
+ The first is the field value; the second is a +CSV::FieldInfo+ object:
533
+ strip_converter = proc {|field, field_info| p field_info; field.strip }
534
+ source = " foo , 0 \n bar , 1 \n baz , 2 \n"
535
+ parsed = CSV.parse(source, converters: strip_converter)
536
+ parsed # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
537
+ Output:
538
+ #<struct CSV::FieldInfo index=0, line=1, header=nil>
539
+ #<struct CSV::FieldInfo index=1, line=1, header=nil>
540
+ #<struct CSV::FieldInfo index=0, line=2, header=nil>
541
+ #<struct CSV::FieldInfo index=1, line=2, header=nil>
542
+ #<struct CSV::FieldInfo index=0, line=3, header=nil>
543
+ #<struct CSV::FieldInfo index=1, line=3, header=nil>
0 commit comments