Created
July 6, 2017 11:35
-
-
Save alexdesi/e319fa1e5cf08e31699bbd66556fab44 to your computer and use it in GitHub Desktop.
Script to convert an array of hashes to a CSV having the name of the columns name equals to the keys of the hashes
This file contains 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 'date' | |
require 'csv' | |
def array_hash_to_csv(data, filename = "data.csv") | |
CSV.open(filename, "wb") do |csv| | |
csv << data.first.keys | |
data.each do |hash| | |
csv << hash.values | |
end | |
end | |
end | |
# Ref: https://stackoverflow.com/questions/17325792/convert-array-of-hashes-to-csv-file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment