ruby-changes:63724
From: Burdette <ko1@a...>
Date: Tue, 24 Nov 2020 09:34:32 +0900 (JST)
Subject: [ruby-changes:63724] 4be336b1b7 (master): [ruby/csv] Recipes for field converters (#177)
https://git.ruby-lang.org/ruby.git/commit/?id=4be336b1b7 From 4be336b1b70168285455bb65f36268555dd5cc20 Mon Sep 17 00:00:00 2001 From: Burdette Lamar <BurdetteLamar@Y...> Date: Sun, 20 Sep 2020 16:38:40 -0500 Subject: [ruby/csv] Recipes for field converters (#177) https://github.com/ruby/csv/commit/aea896f030 diff --git a/doc/csv/recipes.rdoc b/doc/csv/recipes.rdoc index 1ba3c44..20c4c4b 100644 --- a/doc/csv/recipes.rdoc +++ b/doc/csv/recipes.rdoc @@ -1,5 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/doc/csv/recipes.rdoc#L1 == Recipes +All code snippets on this page assume that the following has been executed: + require 'csv' + === Contents - {Parsing: Source Formats}[#label-Parsing-3A+Source+Formats] @@ -12,6 +15,10 @@ https://github.com/ruby/ruby/blob/trunk/doc/csv/recipes.rdoc#L15 - {Parse from IO Stream}[#label-Parse+from+IO+Stream] - {Parse from IO Stream Without Headers}[#label-Parse+from+IO+Stream+Without+Headers] - {Parse from IO Stream with Headers}[#label-Parse+from+IO+Stream+with+Headers] +- {Parsing: Field Converters}[#label-Parsing-3A+Field+Converters] + - {Convert Fields to Objects}[#label-Convert+Fields+to+Objects] + - {Convert Fields to Objects Using Built-In Converters}[#label-Convert+Fields+to+Objects+Using+Built-In+Converters] + - {Convert Fields to Objects Using Custom Converters}[#label-Convert+Fields+to+Objects+Using+Custom+Converters] - {Generating: Output Formats}[#label-Generating-3A+Output+Formats] - {Generate to String}[#label-Generate+to+String] - {Generate to String Without Headers}[#label-Generate+to+String+Without+Headers] @@ -152,6 +159,52 @@ Output: https://github.com/ruby/ruby/blob/trunk/doc/csv/recipes.rdoc#L159 #<CSV::Row "Name":"bar" "Value":"1"> #<CSV::Row "Name":"baz" "Value":"2"> +=== Parsing: Field Converters + +==== Convert Fields to Objects + +Use field converters to change parsed Strings into other, more specific, object. + +==== Convert Fields to Objects Using Built-In Converters + +Without converters (all fields parsed as Strings): + source = "0,1.1,2020-09-19" + parsed = CSV.parse(source) + parsed # => [["0", "1.1", "2020-09-19"]] + parsed.first.each {|field| p field.class } +Output: + String + String + String + +With built-in converters (see {Built-In Field Converters}[../../CSV.html#class-CSV-label-Built-In+Field+Converters]): + parsed = CSV.parse(source, converters: :all) + parsed # => [[0, 1.1, #<DateTime: 2020-09-19T00:00:00+00:00 ((2459112j,0s,0n),+0s,2299161j)>]] + parsed.first.each {|field| p field.class } +Output: + Integer + Float + DateTime + +==== Convert Fields to Objects Using Custom Converters + +Define a custom field converter: + strip_converter = proc {|field| field.strip } + +Without the new converter: + string = " foo , 0 \n bar , 1 \n baz , 2 \n" + array = CSV.parse(string) + array # => [[" foo ", " 0 "], [" bar ", " 1 "], [" baz ", " 2 "]] + +With the new converter: + array = CSV.parse(string, converters: strip_converter) + array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] + +You can also register a custom field converter, then refer to it by name: + CSV::Converters[:strip] = strip_converter + array = CSV.parse(string, converters: :strip) + array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] + === Generating: Output Formats ==== Generate to \String Without Headers -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/