8000 RDoc recipes for diagnostics (#186) · ruby/csv@d9e6791 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9e6791

Browse files
RDoc recipes for diagnostics (#186)
1 parent bee48b0 commit d9e6791

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

doc/csv/recipes/parsing.rdoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ All code snippets on this page assume that the following has been executed:
5858
- {Using Multiple Header Converters}[#label-Using+Multiple+Header+Converters]
5959
- {Recipe: Specify Multiple Header Converters in Option :header_converters}[#label-Recipe-3A+Specify+Multiple+Header+Converters+in+Option+-3Aheader_converters]
6060
- {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]
6164

6265
=== Source Formats
6366

@@ -507,3 +510,34 @@ Apply multiple header converters by defining and registering a custom header con
507510
source = "NAME,VALUE\nfoo,0\nbar,1.0\nbaz,2.0\n"
508511
parsed = CSV.parse(source, headers: true, header_converters: :my_header_converters)
509512
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

Comments
 (0)
0