Bird
0
0

The following Ruby code is intended to print the array ["1", "2", "3"] with quotes and a newline, but it does not work as expected:

medium📝 Debug Q14 of 15
Ruby - Basics and Runtime
The following Ruby code is intended to print the array ["1", "2", "3"] with quotes and a newline, but it does not work as expected:
puts ["1", "2", "3"]
What is the best fix?
AReplace <code>puts</code> with <code>print</code>
BAdd <code>.to_s</code> to the array
CReplace <code>puts</code> with <code>p</code>
DUse <code>puts ["1", "2", "3"].inspect</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand puts ["1", "2", "3"] output

    puts prints each element on a new line without quotes, so output is: 1 2 3
  2. Step 2: Use p to print with quotes and inspect form

    p ["1", "2", "3"] prints the array as it is, including brackets and quotes if strings, with a newline.
  3. Final Answer:

    Replace puts with p -> Option C
  4. Quick Check:

    p prints inspect form with quotes = B [OK]
Quick Trick: Use p to see exact object form with quotes [OK]
Common Mistakes:
  • Using puts expecting quotes
  • Using print which prints without newline
  • Adding to_s which loses inspect format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes