Skip to content

Instantly share code, notes, and snippets.

@bdcravens
Created October 7, 2014 20:30
Show Gist options
  • Save bdcravens/cb76fdad570f80c45940 to your computer and use it in GitHub Desktop.
Save bdcravens/cb76fdad570f80c45940 to your computer and use it in GitHub Desktop.
csv contents to array of hashes
# contents of csv:
# a,b,c
# 456,"foo",0
# 123,"bar",1
# 789,"bam",2
require 'csv'
# iterating through array
result = []
CSV.foreach('./test.csv', {headers:true, header_converters: :symbol}) do |row|
result << row.to_hash
end
puts result.inspect
# as one liner
result = CSV.read('./test.csv', {headers:true, header_converters: :symbol})
.map {|row| row.to_hash }
puts result.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment