Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Enumerable and Collection Processing
What will this Ruby code output?
words = ["dog", "cat", "bird", "fish"]
result = words.detect { |w| w.length > 3 }
puts result
Afish
Bdog
Cbird
Dcat
Step-by-Step Solution
Solution:
  1. Step 1: Check each word's length

    "dog" length 3 (no), "cat" length 3 (no), "bird" length 4 (yes)
  2. Step 2: detect returns first matching element

    "bird" is the first word longer than 3 characters, so it is returned and printed.
  3. Final Answer:

    bird -> Option C
  4. Quick Check:

    detect returns first longer than 3 = bird [OK]
Quick Trick: detect returns first element matching condition [OK]
Common Mistakes:
  • Choosing first element regardless of condition
  • Confusing detect with select
  • Assuming it returns all matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes