Bird
0
0

Identify the issue in the following Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Enumerable and Collection Processing
Identify the issue in the following Ruby code snippet:
values = [10, 15, 20, 25]
groups = values.group_by do |v|
  if v % 2 == 0
    'even'
  else
    'odd'
  end
end
puts groups
AMissing <code>end</code> for the <code>group_by</code> block
BIncorrect use of <code>group_by</code> without a block
CUsing strings instead of symbols as keys causes error
DNo error; code runs correctly
Step-by-Step Solution
Solution:
  1. Step 1: Analyze block structure

    The group_by block starts with do |v| and contains an if-else statement.
  2. Step 2: Check for matching end keywords

    The if statement has an end, and the group_by block also has a closing end.
  3. Step 3: Identify the error

    There is no missing end; the code is syntactically correct and runs without error.
  4. Final Answer:

    No error; code runs correctly -> Option D
  5. Quick Check:

    Count end keywords carefully [OK]
Quick Trick: Ensure all blocks have matching 'end' keywords [OK]
Common Mistakes:
  • Forgetting to close the block with an 'end'
  • Confusing block end with if statement end
  • Assuming strings as keys cause errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes