Bird
0
0

Find the bug in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Basics and Runtime
Find the bug in this Ruby code snippet:
numbers = [1, 2, 3]
numbers.each do |n|
  n * 2
end
puts numbers.inspect
ASyntax error in block definition
BThe multiplication operator * is invalid here
CThe array numbers is unchanged because each does not modify elements
Dnumbers.inspect will print doubled values
Step-by-Step Solution
Solution:
  1. Step 1: Understand each method behavior

    each iterates but does not change the original array.
  2. Step 2: Check output of numbers.inspect

    Since numbers is unchanged, it prints original array [1, 2, 3].
  3. Final Answer:

    The array numbers is unchanged because each does not modify elements -> Option C
  4. Quick Check:

    each does not modify array = original array printed [OK]
Quick Trick: Use map to transform arrays, not each [OK]
Common Mistakes:
MISTAKES
  • Expecting each to change array elements
  • Confusing syntax of block
  • Thinking * operator is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes