Skip to content

Instantly share code, notes, and snippets.

@bleything
Created March 11, 2010 19:21
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bleything/329552 to your computer and use it in GitHub Desktop.
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