Bird
0
0

Which of the following is the correct syntax to open a CSV file for writing in Ruby?

easy📝 Syntax Q12 of 15
Ruby - File IO
Which of the following is the correct syntax to open a CSV file for writing in Ruby?
ACSV.read('file.csv', 'w')
BCSV.open('file.csv', 'r') do |csv| ... end
CCSV.foreach('file.csv') do |csv| ... end
DCSV.open('file.csv', 'w') do |csv| ... end
Step-by-Step Solution
Solution:
  1. Step 1: Identify mode for writing

    To write to a CSV file, the mode should be 'w' (write mode).
  2. Step 2: Match syntax with CSV.open

    The correct syntax is CSV.open('file.csv', 'w') do |csv| ... end to open for writing.
  3. Final Answer:

    CSV.open('file.csv', 'w') do |csv| ... end -> Option D
  4. Quick Check:

    Open with 'w' writes CSV [OK]
Quick Trick: Use 'w' mode with CSV.open to write files [OK]
Common Mistakes:
  • Using 'r' mode which is for reading
  • Using CSV.foreach for writing
  • Using CSV.read with mode argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes