Bird
0
0

What does the Ruby method gsub do when used with a regular expression?

easy📝 Conceptual Q11 of 15
Ruby - Regular Expressions
What does the Ruby method gsub do when used with a regular expression?
AIt returns the length of the string.
BIt replaces all parts of the string that match the regex with a specified replacement.
CIt deletes the string entirely.
DIt splits the string into an array based on the regex.
Step-by-Step Solution
Solution:
  1. Step 1: Understand gsub purpose

    The gsub method is used to replace parts of a string that match a pattern.
  2. Step 2: Recognize regex usage

    When used with a regex, it finds all matches and replaces them with the given replacement string.
  3. Final Answer:

    It replaces all parts of the string that match the regex with a specified replacement. -> Option B
  4. Quick Check:

    gsub with regex = replace all matches [OK]
Quick Trick: Remember: gsub replaces all regex matches in the string [OK]
Common Mistakes:
  • Thinking gsub deletes the string
  • Confusing gsub with split
  • Assuming gsub returns string length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes