Bird
0
0

The following Ruby code is intended to add the line "Goodbye" to the end of log.txt. What is the error?

medium📝 Debug Q14 of 15
Ruby - File IO
The following Ruby code is intended to add the line "Goodbye" to the end of log.txt. What is the error?
File.open('log.txt', 'w') do |file|
  file.puts 'Goodbye'
end
AThe file name should be in double quotes, not single quotes.
BUsing 'w' mode erases the file instead of appending.
CMissing a block parameter for the file object.
DThe method 'puts' cannot be used inside File.open.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the mode used

    The code uses 'w' mode which overwrites the file content.
  2. Step 2: Understand the intended action

    To add content without erasing, 'a' mode should be used instead of 'w'.
  3. Final Answer:

    Using 'w' mode erases the file instead of appending. -> Option B
  4. Quick Check:

    Append needs 'a', not 'w' [OK]
Quick Trick: Use 'a' to add lines, 'w' erases file [OK]
Common Mistakes:
  • Confusing 'w' with append mode
  • Thinking 'w' adds content without erasing
  • Believing quotes style affects file opening

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes