Bird
0
0

What will the following Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Regular Expressions
What will the following Ruby code output?
pattern = /\w+@w+\.com/
email = "user@example.com"
puts email.match?(pattern)
Aerror
Bfalse
Cnil
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex pattern

    The pattern /\w+@\w+\.com/ matches strings like 'word@word.com'.
  2. Step 2: Check if email matches pattern

    "user@example.com" fits the pattern: 'user' + '@' + 'example' + '.com'.
  3. Step 3: Understand match? method

    match? returns true if pattern matches, false otherwise.
  4. Final Answer:

    true -> Option D
  5. Quick Check:

    match? returns true for matching regex [OK]
Quick Trick: Use match? to get true/false for regex match [OK]
Common Mistakes:
  • Confusing match? with match returning MatchData
  • Forgetting to escape dot in regex
  • Expecting string output instead of boolean

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes