Bird
0
0

Which of the following is the correct syntax to replace all digits in a string with "#" using gsub and regex in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Regular Expressions
Which of the following is the correct syntax to replace all digits in a string with "#" using gsub and regex in Ruby?
Astring.gsub("\d", "#")
Bstring.gsub(/d/, "#")
Cstring.gsub(/\d/, "#")
Dstring.gsub("[0-9]", "#")
Step-by-Step Solution
Solution:
  1. Step 1: Identify regex syntax in Ruby

    Regex patterns in Ruby are enclosed in slashes, like /pattern/.
  2. Step 2: Match digits correctly

    The regex /\d/ matches any digit character. Using it with gsub replaces all digits.
  3. Final Answer:

    string.gsub(/\d/, "#") -> Option C
  4. Quick Check:

    Regex in slashes + \d for digits = string.gsub(/\d/, "#") [OK]
Quick Trick: Use slashes / / for regex, not quotes " " [OK]
Common Mistakes:
  • Using quotes instead of slashes for regex
  • Using wrong regex like /d/ instead of /\d/
  • Not escaping backslash properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes