Skip to content

Instantly share code, notes, and snippets.

@l0gicpath
Created May 10, 2016 20:26
Show Gist options
  • Save l0gicpath/0b95b01da8174e5e6f1b784cbe07ef65 to your computer and use it in GitHub Desktop.
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
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