Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - File IO
What will be the output of this Ruby code?
require 'csv'
CSV.foreach('data.csv') do |row|
  puts row.inspect
end

Assuming data.csv contains:
name,age
Alice,30
Bob,25
AError: CSV.foreach requires a block
B["name", "age"]\n["Alice", "30"]\n["Bob", "25"]
C["name,age"]\n["Alice,30"]\n["Bob,30"]
Dname,age\nAlice,30\nBob,25
Step-by-Step Solution
Solution:
  1. Step 1: Understand CSV.foreach behavior

    It reads each line and yields an array of strings split by commas.
  2. Step 2: Analyze puts row.inspect output

    Using puts row.inspect prints the array representation of each row.
  3. Final Answer:

    ["name", "age"]\n["Alice", "30"]\n["Bob", "25"] -> Option B
  4. Quick Check:

    CSV.foreach yields arrays, inspect shows arrays [OK]
Quick Trick: CSV.foreach yields arrays; inspect shows array format [OK]
Common Mistakes:
  • Expecting raw CSV string output
  • Confusing puts with print
  • Missing block for CSV.foreach

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes