Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - File IO
Identify the error in this Ruby code snippet:
require 'csv'
CSV.foreach('data.csv') do |row|
  puts row
end
ANo error, code is correct
BMissing block parameter for CSV.foreach
CCSV.foreach requires a file mode argument
Drow is not an array, so puts will fail
Step-by-Step Solution
Solution:
  1. Step 1: Check CSV.foreach usage

    CSV.foreach reads each row from the file and yields it as an array to the block.
  2. Step 2: Verify block and puts usage

    The block parameter row is present, and puts can print arrays by calling to_s on each element.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    CSV.foreach with block and puts is valid [OK]
Quick Trick: CSV.foreach yields rows as arrays to the block [OK]
Common Mistakes:
  • Thinking puts can't print arrays
  • Expecting file mode argument in foreach

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes