[前][次][番号順一覧][スレッド一覧]

ruby-changes:63726

From: Burdette <ko1@a...>
Date: Tue, 24 Nov 2020 09:34:37 +0900 (JST)
Subject: [ruby-changes:63726] d48e688f64 (master): [ruby/csv] RDoc recipes for diagnostics (#186)

https://git.ruby-lang.org/ruby.git/commit/?id=d48e688f64

From d48e688f64b8a1913ff9bf44202395bb453de291 Mon Sep 17 00:00:00 2001
From: Burdette Lamar <BurdetteLamar@Y...>
Date: Wed, 21 Oct 2020 02:36:16 -0500
Subject: [ruby/csv] RDoc recipes for diagnostics (#186)

https://github.com/ruby/csv/commit/d9e67918e2

diff --git a/doc/csv/recipes/parsing.rdoc b/doc/csv/recipes/parsing.rdoc
index f7967c2..cdb58e4 100644
--- a/doc/csv/recipes/parsing.rdoc
+++ b/doc/csv/recipes/parsing.rdoc
@@ -58,6 +58,9 @@ All code snippets on this page assume that the following has been executed: https://github.com/ruby/ruby/blob/trunk/doc/csv/recipes/parsing.rdoc#L58
   - {Using Multiple Header Converters}[#label-Using+Multiple+Header+Converters]
     - {Recipe: Specify Multiple Header Converters in Option :header_converters}[#label-Recipe-3A+Specify+Multiple+Header+Converters+in+Option+-3Aheader_converters]
     - {Recipe: Specify Multiple Header Converters in a Custom Header Converter List}[#label-Recipe-3A+Specify+Multiple+Header+Converters+in+a+Custom+Header+Converter+List]
+- {Diagnostics}[#label-Diagnostics]
+  - {Recipe: Capture Unconverted Fields}[#label-Recipe-3A+Capture+Unconverted+Fields]
+  - {Recipe: Capture Field Info}[#label-Recipe-3A+Capture+Field+Info]
 
 === Source Formats
 
@@ -507,3 +510,34 @@ Apply multiple header converters by defining and registering a custom header con https://github.com/ruby/ruby/blob/trunk/doc/csv/recipes/parsing.rdoc#L510
   source = "NAME,VALUE\nfoo,0\nbar,1.0\nbaz,2.0\n"
   parsed = CSV.parse(source, headers: true, header_converters: :my_header_converters)
   parsed.headers # => [:name, :value]
+
+=== Diagnostics
+
+==== Recipe: Capture Unconverted Fields
+
+To capture unconverted field values, use option +:unconverted_fields+:
+  source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
+  parsed = CSV.parse(source, converters: :integer, unconverted_fields: true)
+  parsed # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
+  parsed.each {|row| p row.unconverted_fields }
+Output:
+  ["Name", "Value"]
+  ["foo", "0"]
+  ["bar", "1"]
+  ["baz", "2"]
+
+==== Recipe: Capture Field Info
+
+To capture field info in a custom converter, accept two block arguments.
+The first is the field value; the second is a +CSV::FieldInfo+ object:
+  strip_converter = proc {|field, field_info| p field_info; field.strip }
+  source = " foo , 0 \n bar , 1 \n baz , 2 \n"
+  parsed = CSV.parse(source, converters: strip_converter)
+  parsed # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
+Output:
+  #<struct CSV::FieldInfo index=0, line=1, header=nil>
+  #<struct CSV::FieldInfo index=1, line=1, header=nil>
+  #<struct CSV::FieldInfo index=0, line=2, header=nil>
+  #<struct CSV::FieldInfo index=1, line=2, header=nil>
+  #<struct CSV::FieldInfo index=0, line=3, header=nil>
+  #<struct CSV::FieldInfo index=1, line=3, header=nil>
\ No newline at end of file
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]