Created
April 10, 2019 13:59
-
-
Save jrgifford/b0cad6338f1e7dc3d99b1e551d32ccd6 to your computer and use it in GitHub Desktop.
Ruby CSV library is smart and will select just keys in the headers if given headers and a hash
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 'csv' | |
filename = "/tmp/csv_test.csv" | |
things_to_write = [ | |
{a: "a", b: "b", c: "c"}, | |
{c: "c", b: "b", a: "a", does_not: "exist"}, | |
{a: "a", c: "c"} | |
] | |
CSV.open(filename, "wb", headers: things_to_write.first.keys) do |csv| | |
things_to_write.each do |thing| | |
csv << thing | |
end | |
end | |
`cat #{filename}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment