Bird
0
0

You want to print a nested array in Ruby so that each element is shown with quotes and the entire structure is visible on one line. Which code snippet achieves this?

hard📝 Application Q15 of 15
Ruby - Basics and Runtime
You want to print a nested array in Ruby so that each element is shown with quotes and the entire structure is visible on one line. Which code snippet achieves this?
Ap [["1", "2"], ["3", "4"]]
Bprint [["1", "2"], ["3", "4"]]
Cputs [["1", "2"], ["3", "4"]].to_s
Dputs [["1", "2"], ["3", "4"]]
Step-by-Step Solution
Solution:
  1. Step 1: Analyze puts [["1", "2"], ["3", "4"]]

    This prints each inner array on a separate line without quotes, so output is: [1, 2] [3, 4]
  2. Step 2: Analyze print [["1", "2"], ["3", "4"]]

    This prints the array on one line but without quotes around elements, so output is: [[1, 2], [3, 4]] (no quotes)
  3. Step 3: Analyze p [["1", "2"], ["3", "4"]]

    p prints the inspect form, showing quotes if strings and the full structure on one line, exactly as the array looks.
  4. Step 4: Analyze puts [["1", "2"], ["3", "4"]].to_s

    This converts the array to string and prints it with a newline, but inner elements are not quoted as inspect would show.
  5. Final Answer:

    p [["1", "2"], ["3", "4"]] -> Option A
  6. Quick Check:

    p prints full inspect form on one line = D [OK]
Quick Trick: Use p to print full structure with quotes on one line [OK]
Common Mistakes:
  • Using puts which prints each element on separate lines
  • Using print which lacks quotes
  • Using to_s which loses inspect formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes