Created
March 11, 2010 19:21
-
-
Save bleything/329552 to your computer and use it in GitHub Desktop.
an example of Hash[ *ary1.zip(ary2).flatten ]
This file contains hidden or 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
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