CSV
Read rows
The csv library parses comma separated files without manual splitting. Require csv once and read the whole file into nested arrays.
Each inner array is one row, so rows zero holds the header line when the
file starts with column names. Ruby 3.4 no longer ships csv
as a default gem, so gem install csv comes first on
that version.
require "csv" rows = CSV.read("data.csv") puts rows.inspect
Headers on
Positional indexes break as soon as columns move. Turn headers on and address every value by column name instead.
Foreach streams one row at a time, which keeps memory flat even for large exports.
require "csv" CSV.foreach("data.csv", headers: true) do |row| puts row["name"] end
Write CSV
Writing mirrors reading. Open a file for writing and push one array per row into the writer object.
The library adds quotes and escapes only where needed, so commas inside values stay safe.
require "csv" CSV.open("out.csv", "w") do |csv| csv << ["name", "age"] csv << ["Ada", 36] end
Converters
Every CSV value arrives as text by default. Pass the numeric converter to turn digit columns into Integer and Float values.
Header names stay strings while number cells arrive converted, which removes a whole layer of manual conversion calls.
require "csv" CSV.foreach("data.csv", headers: true, converters: :numeric) do |row| puts row["age"].class end
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
CSV.read("data.csv")return?Answer
Nested arrays, one inner array per row
Each row becomes an array of its cells, and the rows are kept in order.
-
What changes when
CSV.foreachis givenheaders: true?Answer
Each row is read by column name instead of by position
Named columns survive a column moving, while positional indexes break.
-
Why does the lesson prefer
CSV.foreachfor a large export?Answer
It streams one row at a time, so memory stays flat
CSV.read holds the whole file in memory; foreach does not.
-
What does
converters: :numericdo to the cells?Answer
Turns digit cells into Integer and Float values
Header names stay strings, and the number cells arrive converted.
-
Why does a Ruby 3.4 program need
gem install csv?Answer
csv is no longer a default gem in Ruby 3.4
The require fails with a warning that says exactly that, and the gem has to be installed.
-
What does
csv << ["Ada", 36]do insideCSV.open("out.csv", "w")?Answer
Writes one row to the file
The writer turns the array into one CSV line, quoting only what needs it.
-
Why are positional indexes fragile in a CSV reader?
Answer
A column inserted or moved shifts every later index
That is the reason headers are turned on for anything but a throwaway script.
-
Without headers, what is one row from
CSV.foreach?Answer
An array of strings
The plain form hands each row over as an array, which is read by position.
-
What does the csv library do with a value that contains a comma?
Answer
Quotes and escapes it so it stays one value
That is the part of the format a manual
split(",")gets wrong. -
What type does a CSV cell have by default?
Answer
String
Every value arrives as text until a converter is asked for.
Article author: Arthur Isaev