Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - File IO
What will this Ruby code output?
require 'csv'
CSV.open('test.csv', 'w') do |csv|
  csv << ['Name', 'Age']
  csv << ['John', 22]
end
puts File.read('test.csv')
AName Age\nJohn 22
B['Name', 'Age']\n['John', 22]
CName,Age\nJohn,22\n
DError: File not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand CSV.open with 'w'

    This opens 'test.csv' for writing and writes two rows as arrays, which become CSV lines.
  2. Step 2: Reading the file content

    File.read reads the file as text, showing comma-separated values with newlines.
  3. Final Answer:

    Name,Age\nJohn,22\n -> Option C
  4. Quick Check:

    CSV.open writes CSV lines correctly [OK]
Quick Trick: CSV.open with 'w' writes arrays as CSV lines [OK]
Common Mistakes:
  • Expecting array syntax in output
  • Confusing spaces with commas

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes