Heads up: This description was created by AI and might not be 100% accurate.
read_with_new.rb
This Ruby code snippet demonstrates reading a CSV file (file.csv
) and printing each header-value pair from each row. It uses the CSV
library to parse the file, assuming the first row contains headers. The code iterates through each row and then through each header-value pair in that row, printing them to the console, followed by a newline after each row’s output.
Ruby code snippet
require 'csv'
#=> true
File.open("input/csv/file.csv", "r") do |f|
csv = CSV.new(f, headers: true)
csv.each do |line|
line.each do |header, val|
p [header, val]
end
puts
end
end
["key", "key1"]
["value", "value1"]
["key", "key2"]
["value", "value2"]
["key", "key3"]
["value", "value3"]
#=> nil
Executed with Ruby 3.4.5
.