Reputation: 3663
I created a CSV file named "file.csv"
that looks like this:
Artist,Album
I want to use Ruby's CSV library to write to it. But I want to write to the file using its header names. Something like this:
CSV.open("file.csv",'a'){|file|
file["Artist"] = "foo"
file["Album"] = "bar"
}
How do I do this with Ruby's CSV library?
Upvotes: 3
Views: 986
Reputation: 44
header = ["Artist","Album"]
CSV.open("file.csv","a") do |csv|
row = CSV::Row.new(header,[])
row["Artist"] = "foo"
row["Album"] = "bar"
csv << row
end
Upvotes: 2