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.

  1. 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.

  2. What changes when CSV.foreach is given headers: true?

    Answer

    Each row is read by column name instead of by position

    Named columns survive a column moving, while positional indexes break.

  3. Why does the lesson prefer CSV.foreach for 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.

  4. What does converters: :numeric do to the cells?

    Answer

    Turns digit cells into Integer and Float values

    Header names stay strings, and the number cells arrive converted.

  5. 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.

  6. What does csv << ["Ada", 36] do inside CSV.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.

  7. 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.

  8. 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.

  9. 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.

  10. 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