Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Loops and Iteration
Find the error in this Ruby code snippet:
array = [10, 20, 30]
array.each { |x| x = x + 5 }
puts array.join(',')
ASyntax error in block
BThe array elements are not changed
CMissing return statement
DIncorrect method call
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable assignment inside block

    Assigning x = x + 5 changes only the block variable, not the array elements.
  2. Step 2: Check the output

    The original array remains unchanged, so puts array.join(',') prints '10,20,30'.
  3. Final Answer:

    The array elements are not changed -> Option B
  4. Quick Check:

    Block variable assignment does not modify original array [OK]
Quick Trick: Block vars are copies; modify array elements directly [OK]
Common Mistakes:
  • Expecting block assignment to change array
  • Confusing block variable with array element reference
  • Not using map for transformation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes