Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sean Cribbs http://seancribbs.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Turn CSV with headers into Array of Hashes (in 5 lines or less)

This assumes you have a CSV file whose first line are headings/labels for the individual columns.

require 'csv'

csv_data = CSV.read 'data.csv'
headers = csv_data.shift.map {|i| i.to_s }
string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS