-
-
Save sahidursuman/43ec13c08f96bc3b05b70e7c2ff32ddc to your computer and use it in GitHub Desktop.
Ruby hash array to CSV
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
class Array | |
def to_csv(csv_filename="hash.csv") | |
require 'csv' | |
CSV.open(csv_filename, "wb") do |csv| | |
csv << first.keys # adds the attributes name on the first line | |
self.each do |hash| | |
csv << hash.values | |
end | |
end | |
end | |
end | |
# ex: [{a: 1, b: 2}, {a: 3, b: 4}].to_csv("hash.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment