0
0
Rubyprogramming~5 mins

CSV library basics in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the CSV library in Ruby?
The CSV library helps read from and write to CSV (Comma-Separated Values) files easily, making it simple to handle tabular data.
Click to reveal answer
beginner
How do you require the CSV library in a Ruby script?
Use require 'csv' at the top of your Ruby file to load the CSV library.
Click to reveal answer
beginner
How do you read a CSV file line by line in Ruby?
Use CSV.foreach('filename.csv') do |row| ... end. Each row is an array of values from that line.
Click to reveal answer
intermediate
How do you write an array of arrays to a CSV file in Ruby?
Use CSV.open('filename.csv', 'w') do |csv| data.each { |row| csv << row } end where data is an array of arrays.
Click to reveal answer
intermediate
What does CSV.parse do?
CSV.parse reads CSV data from a string instead of a file, returning an array of rows.
Click to reveal answer
Which Ruby command loads the CSV library?
Arequire 'csv'
Bimport csv
Cinclude CSV
Dload 'csv'
What does CSV.foreach('file.csv') do |row| ... end do?
AReads the CSV file line by line, yielding each row as an array
BWrites rows to the CSV file
CParses CSV data from a string
DDeletes the CSV file
How do you write data to a CSV file in Ruby?
ACSV.delete('file.csv')
BCSV.read('file.csv')
CCSV.open('file.csv', 'w') { |csv| csv << ['data'] }
DCSV.parse('data')
What type of object is each row when reading CSV with CSV.foreach?
AString
BArray
CHash
DInteger
Which method reads CSV data from a string instead of a file?
ACSV.foreach
BCSV.read
CCSV.open
DCSV.parse
Explain how to read a CSV file line by line in Ruby using the CSV library.
Think about how to open and process each line easily.
You got /4 concepts.
    Describe the steps to write multiple rows of data to a CSV file in Ruby.
    Consider how to open a file for writing and add rows one by one.
    You got /4 concepts.