Complete the code to require the CSV library.
require '[1]'
The CSV library is included by requiring 'csv'. This makes CSV methods available.
Complete the code to read a CSV file named 'data.csv' and print each row.
CSV.foreach('data.csv') do |[1]| puts [1].inspect end
CSV.foreach reads each row and yields it to the block as 'row'.
Fix the error in the code to parse CSV data from a string.
data = "name,age\nAlice,30\nBob,25" rows = CSV.[1](data) rows.each { |row| puts row[0] }
CSV.parse reads CSV data from a string. CSV.foreach is for files.
Fill both blanks to create a hash from CSV rows with headers.
CSV.foreach('data.csv', headers: true) do |[1]| person = [2].to_h puts person end
Each row is yielded as 'row'. Calling to_h converts it to a hash with headers as keys.
Fill all three blanks to write CSV data with headers.
CSV.open('output.csv', 'w', write_headers: true, headers: [[1]]) do |csv| csv.[2]([[3]]) end
Headers are given as an array of strings. To add a row, use the '<<' operator with an array of values.
