Bird
0
0

How can you find the first string in an array arr that starts with a vowel and has length 5 using detect?

hard📝 Application Q9 of 15
Ruby - Enumerable and Collection Processing
How can you find the first string in an array arr that starts with a vowel and has length 5 using detect?
Aarr.detect { |s| s.match?(/^[aeiou]/i) && s.length == 5 }
Barr.detect { |s| s.start_with?('a','e','i','o','u') && s.size == 5 }
Carr.find { |s| s[0] =~ /[aeiou]/ && s.length == 5 }
DAll of the above
Step-by-Step Solution
Solution:
  1. Step 1: Understand different ways to check starting vowel

    Regex with match?, pattern match with =~, and start_with? all check first letter.
  2. Step 2: Confirm length checks are correct

    Using length or size both measure string length correctly.
  3. Step 3: All options correctly find first string matching both conditions

    Therefore, all options are valid ways to solve the problem.
  4. Final Answer:

    All of the above -> Option D
  5. Quick Check:

    Multiple valid methods for condition = D [OK]
Quick Trick: Multiple Ruby methods can check string start and length [OK]
Common Mistakes:
  • Assuming only one method works
  • Confusing length and size
  • Incorrect regex syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes