Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Enumerable and Collection Processing
Identify the error in this Ruby code:
a = [1, 2, 3]
b = ["x", "y"]
result = a.zip b
puts result
AMissing parentheses in zip call causes syntax error
BNo error; code runs and prints zipped array
CUsing puts on array prints object id instead of content
Dzip method requires block, so code fails
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of zip call without parentheses

    Ruby allows method calls without parentheses if arguments are present.
  2. Step 2: Check output of puts on array

    puts prints each element on a new line, so it works fine here.
  3. Final Answer:

    No error; code runs and prints zipped array -> Option B
  4. Quick Check:

    zip without parentheses is valid = no error [OK]
Quick Trick: Parentheses optional for single method argument [OK]
Common Mistakes:
  • Assuming parentheses are mandatory
  • Expecting puts to print array as object id
  • Thinking zip needs a block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes