Bird
0
0

Find the mistake in this code:

medium📝 Debug Q7 of 15
Ruby - Regular Expressions
Find the mistake in this code:
str = "abc123"
str.gsub(/\d+/, '#')
puts str
AReplacement string must be double quotes, not single quotes
Bgsub does not modify str in place, so str remains unchanged
CRegex pattern is incorrect, should be /\d*/
Dputs cannot print the result of gsub
Step-by-Step Solution
Solution:
  1. Step 1: Understand gsub behavior

    gsub returns a new string and does not change the original string unless gsub! is used.
  2. Step 2: Check code output

    Since str is unchanged, puts str prints the original string.
  3. Final Answer:

    gsub does not modify str in place, so str remains unchanged -> Option B
  4. Quick Check:

    gsub returns new string, original unchanged unless gsub! used [OK]
Quick Trick: Use gsub! to modify string in place [OK]
Common Mistakes:
  • Expecting gsub to change original string
  • Confusing gsub and gsub! methods
  • Thinking single vs double quotes affect replacement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes