Created
May 10, 2016 20:26
-
-
Save l0gicpath/0b95b01da8174e5e6f1b784cbe07ef65 to your computer and use it in GitHub Desktop.
Importing from CSV into an array of hashed values mapped to csv fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'CSV' | |
class ImportCSV | |
class << self | |
def parse csv_file | |
CSV::Converters[:blank_nil] = lambda do |field| | |
field && field.empty? ? nil : field | |
end | |
csv_data = CSV.read(csv_file, :headers => true, | |
:header_converters => :symbol, | |
:converters => [:all, :blank_nil]) | |
csv_data.map { |row| row.to_hash } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment