Created
October 7, 2014 20:30
-
-
Save bdcravens/cb76fdad570f80c45940 to your computer and use it in GitHub Desktop.
csv contents to array of hashes
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
# 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