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:
numbers = [1, 2, 3]
new_numbers = numbers.map do |n|
n * 2
end
puts new_numbers
AMissing parentheses in map call
BNo error; code works correctly
CBlock variable syntax is incorrect
Dmap does not return a new array
Step-by-Step Solution
Solution:
  1. Step 1: Check block syntax with do...end

    The block uses correct syntax with do |n| ... end.

  2. Step 2: Confirm map returns new array

    map returns a new array with each element doubled.

  3. Final Answer:

    No error; code works correctly -> Option B
  4. Quick Check:

    map with do...end block is valid [OK]
Quick Trick: map accepts do...end blocks as well as braces [OK]
Common Mistakes:
  • Thinking parentheses are mandatory
  • Confusing block variable syntax
  • Assuming map modifies original array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes