Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Regular Expressions
What will this Ruby code print?
text = "hello world"
result = text.match?(/world$/)
puts result
AError
Bfalse
Cnil
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Understand match? with regex /world$/

    The regex /world$/ checks if the string ends with "world". match? returns true or false.
  2. Step 2: Check the string

    "hello world" ends with "world", so match? returns true, which is printed.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    match? returns boolean for pattern presence [OK]
Quick Trick: Use match? to get true/false if pattern matches [OK]
Common Mistakes:
  • Expecting match? to return MatchData
  • Confusing match? with match
  • Thinking it returns nil if no match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes