I have the following code:
def csv_to_array(file)
csv = CSV::parse(file)
fields = csv.shift
array = csv.collect { |record| Hash[*fields.zip(record).flatten] }
end
This creates an array of hashes, and works fine with comma separated values. I am trying to replicate this code for a tab delimited file. Currently, when I run the above code on my tab delimited file, I get something like this:
array[0] = {"First Name\tLast Name\tCode\t"=>"Luigi\tSmith\t1406\t"}
So, each array object is a hash as intended, but it has one key value pair - The entire tab delimited header row being the key, and the individual row of data being the value.
How can I alter this code to return an array of hashes with individual key value pairs, with the header of each column mapping to the row value for that column?