Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Enumerable and Collection Processing
Identify the error in this Ruby code snippet:
numbers = [1, 2, 3, 4]
filtered = numbers.select do |n|
  n > 2
end
puts filtered
ANo error, code works correctly
BMissing block variable in select
Cselect should be replaced with map
Dputs cannot print arrays directly
Step-by-Step Solution
Solution:
  1. Step 1: Check block syntax and usage

    The block uses do |n| ... end correctly with a block variable.
  2. Step 2: Verify select usage and output

    select filters elements greater than 2, returning [3, 4]. puts prints each element on a new line, which is valid.
  3. Final Answer:

    No error, code works correctly -> Option A
  4. Quick Check:

    Correct block and select usage = No error, code works correctly [OK]
Quick Trick: select with do-end block is valid syntax [OK]
Common Mistakes:
  • Thinking puts can't print arrays
  • Confusing select with map
  • Missing block variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes