Skip to content

Instantly share code, notes, and snippets.

@kspurgin
Created October 2, 2020 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kspurgin/f14d334c9d52ba2a961e64a39465fcec to your computer and use it in GitHub Desktop.
Save kspurgin/f14d334c9d52ba2a961e64a39465fcec to your computer and use it in GitHub Desktop.
Exploring Ruby CSV standard library default handling of empty cell values
2.6.3 :001 > require 'csv'
=> true
2.6.3 :002 > csv = CSV.parse(File.read('required_field_empty.csv'), headers: true)
=> #<CSV::Table mode:col_or_row row_count:4>
2.6.3 :003 > rows = []
=> []
2.6.3 :004 > csv.each{ |r| rows << r.to_h }
=> #<CSV::Table mode:col_or_row row_count:4>
2.6.3 :024 > rows[1]['objectNumber']
=> nil
-=-=-=-=-=-=-
2.6.3 :030 > rows = []
=> []
2.6.3 :031 > csv = CSV.parse(File.read('required_field_empty.csv'), headers: true, nil_value: '')
=> #<CSV::Table mode:col_or_row row_count:4>
2.6.3 :032 > csv.each{ |r| rows << r.to_h }
=> #<CSV::Table mode:col_or_row row_count:4>
2.6.3 :033 > rows[1]['objectNumber']
=> ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment