Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
an example of Hash[ *ary1.zip(ary2).flatten ]
require 'pp'
# say you've loaded a csv file with a header row, but you don't have
# access to any nice csv library. All you've got is an array of rows,
# which are also arrays:
#
# [
# [ a, b, c, d ],
# [ 1, 2, 3, 4 ],
# [ 5, 6, 7, 8 ],
# ...
# ]
#
# the first row is a header row, the rest are data. Then, you do this:
data = [
[ :a, :b, :c, :d ],
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 0, 0, 0, 9 ],
]
headers = data.shift
pp data.map {|row| Hash[ *headers.zip(row).flatten ] }
# ta da!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.