How to rename CSV headers in Ruby?

I have a CSV file and I want to merge these entries into an existing CSV file. However, two files have headers that are named differently. How can I cleanly and efficiently rename CSV headers to combine the file I'm merging with?

+4
source share
1 answer

Answer:

CSV::HeaderConverters[:map_to_main] = lambda do |header|
  # work your magic here
  header
end
CSV.open(file,
  headers: true,
  header_converters: :map_to_main).to_a.map(&:to_hash)
+7
source

All Articles