Working with CSV
The csv library.
Working with CSV is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is CSV?
CSV (Comma-Separated Values) stores tabular data as plain text, one row per line with fields separated by commas. Ruby's standard csv library reads and writes it.
require 'csv'
row = CSV.parse_line('Alice,30,Engineer')
p rowParsing a CSV String
CSV.parse turns a multi-line CSV string into an array of row arrays.
require 'csv'
data = "name,age\nAlice,30\nBob,25"
rows = CSV.parse(data)
p rowsUsing Headers
Pass headers: true so the first row becomes field names and each row behaves like a hash.
require 'csv'
data = "name,age\nAlice,30"
CSV.parse(data, headers: true).each do |row|
puts(row['name'])
puts(row['age'])
endIterating Rows
You can loop through parsed rows like any array, accessing fields by index.
require 'csv'
rows = CSV.parse("1,a\n2,b\n3,c")
rows.each { |r| puts "#{r[0]} -> #{r[1]}" }Converting Types
Use converters: :numeric to automatically turn numeric fields into Integers or Floats.
require 'csv'
rows = CSV.parse("10,20\n30,40", converters: :numeric)
puts(rows[0][0] + rows[1][1])Generating CSV
CSV.generate builds a CSV string. Push arrays into the block to add rows.
require 'csv'
out = CSV.generate do |csv|
csv << ['name', 'age']
csv << ['Alice', 30]
end
puts outQuoting and Special Characters
CSV automatically quotes fields that contain commas, quotes, or newlines so the structure stays intact.
require 'csv'
out = CSV.generate do |csv|
csv << ['note', 'Hello, world']
end
puts outCustom Separators
For tab- or semicolon-separated files, set col_sep. This handles TSV and European CSV variants.
require 'csv'
rows = CSV.parse("a;b;c", col_sep: ';')
p rowsWriting to a File
CSV.open with mode 'w' streams rows directly to a file.
require 'csv'
require 'tmpdir'
path = File.join(Dir.tmpdir, 'people.csv')
CSV.open(path, 'w') do |csv|
csv << ['name', 'age']
csv << ['Carol', 28]
end
puts(File.read(path))Reading from a File
CSV.read loads an entire file into an array of rows, accepting the same options as parse.
require 'csv'
require 'tmpdir'
path = File.join(Dir.tmpdir, 'nums.csv')
File.write(path, "x,y\n1,2\n3,4")
rows = CSV.read(path, headers: true)
puts(rows.first['y'])Summing a Column
A common task: read a column and aggregate it. Combine headers, numeric conversion, and map.
require 'csv'
data = "item,price\npen,3\nbook,12"
total = CSV.parse(data, headers: true, converters: :numeric).sum { |r| r['price'] }
puts(total)Quick Check
Which option lets you access CSV fields by column name like row['age']?
Recap: Working with CSV
You can handle tabular data:
CSV.parse/CSV.readto readheaders: truefor named fieldsconverters: :numericfor typed valuesCSV.generate/CSV.opento writecol_sepfor TSV and other delimiters
require 'csv'
p CSV.parse_line('a,b,c')Frequently asked questions
Is the “Working with CSV” lesson free?
Yes — the full text of “Working with CSV” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Working with CSV”?
The csv library. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Working with CSV” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading and Writing JSON
- Working with CSV
- YAML Configuration
- Choosing a Format