Skip to content

Instantly share code, notes, and snippets.

@alexdesi
Created July 6, 2017 11:35
Show Gist options
  • Save alexdesi/e319fa1e5cf08e31699bbd66556fab44 to your computer and use it in GitHub Desktop.
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
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