From: kou@... Date: 2018-04-09T15:55:56+00:00 Subject: [ruby-core:86484] [Ruby trunk Feature#12839] CSV - Give not nil but empty strings for empty fields Issue #12839 has been updated by kou (Kouhei Sutou). I added `:nil_value` option as a shortcut: ```ruby require "csv" p CSV.parse(',"",a') # => [[nil, "", "a"]] p CSV.parse(',"",a', nil_value: "") # => [["", "", "a"]] ``` But it's not so fast: ```ruby require "csv" require "benchmark/ips" csv_text = <(s) {s || ""} Benchmark.ips do |r| r.report "not convert" do CSV.parse(csv_text) end r.report "converter" do CSV.parse(csv_text, converters: convert_nil) end r.report "option" do CSV.parse(csv_text, nil_value: "") end r.compare! end ``` ```text Warming up -------------------------------------- not convert 742.000 i/100ms converter 620.000 i/100ms option 672.000 i/100ms Calculating ------------------------------------- not convert 7.480k (�� 1.8%) i/s - 37.842k in 5.061095s converter 6.289k (�� 0.5%) i/s - 31.620k in 5.028042s option 6.697k (�� 3.7%) i/s - 33.600k in 5.025273s Comparison: not convert: 7479.8 i/s option: 6696.8 i/s - 1.12x slower converter: 6288.9 i/s - 1.19x slower ``` ---------------------------------------- Feature #12839: CSV - Give not nil but empty strings for empty fields https://bugs.ruby-lang.org/issues/12839#change-71431 * Author: 5.5 (5 5) * Status: Closed * Priority: Normal * Assignee: kou (Kouhei Sutou) * Target version: ---------------------------------------- The CSV parser gives nil for empty fields. ```rb require "csv" CSV.parse(%|,""|) #=> [[nil, ""]] ``` The above behavior maybe be suitable for certain programmers, but I hope to get `[["", ""]]`. So I had used to write the following code reluctantly till Ruby 2.1: ```rb require "csv" CSV.parse(%|,""|, converters: lambda{|v| v || ""}) #=> [["", ""]] ``` It is wasteful, but certainly works for my purpose. However, because of #11126, the above code does not work from Ruby 2.2. (Converters are not called for nil) I merely want an option, which makes the CSV parser give empty strings for empty fields. Namely, ```rb require "csv" CSV.parse(%|,""|, string: true) #=> [["", ""]] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: