Bird
0
0

How would you combine a predicate method valid_email? with a regular expression to check if an email contains '@' and ends with '.com'?

hard📝 Application Q9 of 15
Ruby - Methods
How would you combine a predicate method valid_email? with a regular expression to check if an email contains '@' and ends with '.com'?
AAll of the above
Bdef valid_email?(email) email.include?('@') && email.end_with?('.com') end
Cdef valid_email?(email) email =~ /@.*\.com$/ end
Ddef valid_email?(email) email.match?(/@.*\.com$/) end
Step-by-Step Solution
Solution:
  1. Step 1: Understand regex and string methods

    Predicate methods should return strict booleans (true/false). The question specifies using a regular expression.
  2. Step 2: Analyze options

    B uses string methods (no regex); C uses =~ which returns match position (integer) or nil (not strict boolean); D uses match? with regex, returning true/false directly.
  3. Final Answer:

    def valid_email?(email)\n email.match?(/@.*\.com$/)\nend -> Option D
  4. Quick Check:

    match? with regex returns boolean [OK]
Quick Trick: Use regex or string methods to check patterns in predicates [OK]
Common Mistakes:
  • Confusing =~ return value
  • Using match instead of match?
  • Ignoring string method chaining

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes