Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Regular Expressions
Identify the error in this Ruby code snippet:
sentence = "Hello World!"
sentence.gsub(/world/, "Earth")
What is the problem?
ARegex is case-sensitive, so 'world' does not match 'World'.
BMissing replacement string in gsub.
CSyntax error due to missing parentheses.
Dgsub cannot be used with regex.
Step-by-Step Solution
Solution:
  1. Step 1: Check regex matching

    The regex /world/ is lowercase and Ruby regex is case-sensitive by default.
  2. Step 2: Compare with string content

    The string has "World" with uppercase W, so the regex does not match anything.
  3. Final Answer:

    Regex is case-sensitive, so 'world' does not match 'World'. -> Option A
  4. Quick Check:

    Case sensitivity causes no match [OK]
Quick Trick: Remember regex is case-sensitive unless flagged [OK]
Common Mistakes:
  • Assuming regex matches ignore case by default
  • Thinking gsub needs parentheses around arguments
  • Believing gsub can't use regex

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes