Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Enumerable and Collection Processing
What will be the output of this Ruby code?
words = ['cat', 'car', 'dog', 'deer']
groups = words.group_by { |w| w[0] }
puts groups['c'].inspect
Anil
B['dog', 'deer']
C['cat']
D['cat', 'car']
Step-by-Step Solution
Solution:
  1. Step 1: Group words by first letter

    The block groups words by their first character: 'c' or 'd'.
  2. Step 2: Identify words starting with 'c'

    Words 'cat' and 'car' start with 'c', so groups['c'] is ['cat', 'car'].
  3. Final Answer:

    ['cat', 'car'] -> Option D
  4. Quick Check:

    groups['c'] contains words starting with 'c' [OK]
Quick Trick: Access groups by exact key from block return [OK]
Common Mistakes:
  • Mixing keys 'c' and 'd'
  • Expecting single word instead of array
  • Using wrong index for first letter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes