📖 Scenario: You are working on a text editor feature that needs to clean up user input by replacing certain patterns in a string.
🎯 Goal: Learn how to use Ruby's gsub method with regular expressions to find and replace parts of a text.
📋 What You'll Learn
Create a string variable with a specific sentence.
Create a regular expression pattern to find all vowels.
Use gsub with the regex to replace vowels with a star *.
Print the modified string.
💡 Why This Matters
🌍 Real World
Text editors and input sanitizers often need to find and replace patterns in text, like removing vowels or masking sensitive data.
💼 Career
Understanding how to use regular expressions with string replacement is a valuable skill for software developers working on data cleaning, text processing, and user input validation.
Progress0 / 4 steps
1
Create the initial string
Create a string variable called sentence and set it to the exact value "Hello World! Welcome to Ruby programming."
Ruby
Hint
Use double quotes to create the string exactly as shown.
2
Create a regex pattern for vowels
Create a variable called vowels_regex and set it to a regular expression that matches all vowels (both uppercase and lowercase). Use /[aeiouAEIOU]/ as the pattern.
Ruby
Hint
Use slashes /.../ to create the regex pattern.
3
Replace vowels with stars using gsub
Use the gsub method on sentence with vowels_regex to replace all vowels with the star character '*'. Store the result in a new variable called cleaned_sentence.
Ruby
Hint
Call gsub on sentence with vowels_regex and '*' as arguments.
4
Print the cleaned sentence
Print the variable cleaned_sentence to display the sentence with vowels replaced by stars.
Ruby
Hint
Use puts cleaned_sentence to print the result.
Practice
(1/5)
1. What does the Ruby method gsub do when used with a regular expression?
easy
A. It returns the length of the string.
B. It replaces all parts of the string that match the regex with a specified replacement.
C. It deletes the string entirely.
D. It splits the string into an array based on the regex.
Solution
Step 1: Understand gsub purpose
The gsub method is used to replace parts of a string that match a pattern.
Step 2: Recognize regex usage
When used with a regex, it finds all matches and replaces them with the given replacement string.
Final Answer:
It replaces all parts of the string that match the regex with a specified replacement. -> Option B
Quick Check:
gsub with regex = replace all matches [OK]
Hint: 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
2. Which of the following is the correct syntax to replace all digits in a string with "#" using gsub and regex in Ruby?
easy
A. string.gsub("\d", "#")
B. string.gsub(/d/, "#")
C. string.gsub(/\d/, "#")
D. string.gsub("[0-9]", "#")
Solution
Step 1: Identify regex syntax in Ruby
Regex patterns in Ruby are enclosed in slashes, like /pattern/.
Step 2: Match digits correctly
The regex /\d/ matches any digit character. Using it with gsub replaces all digits.
Final Answer:
string.gsub(/\d/, "#") -> Option C
Quick Check:
Regex in slashes + \d for digits = string.gsub(/\d/, "#") [OK]
Hint: 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
3. What is the output of this Ruby code?
text = "Call me at 123-456-7890."
result = text.gsub(/\d/, "*")
puts result
medium
A. Call me at ***-***-****.
B. Call me at 123-456-7890.
C. Call me at \d\d\d-\d\d\d-\d\d\d\d.
D. SyntaxError
Solution
Step 1: Understand the regex and replacement
The regex /\d/ matches every digit character. The replacement is "*".
Step 2: Apply gsub to replace digits
Every digit in the string is replaced by "*", so numbers become stars but other characters stay the same.
Final Answer:
Call me at ***-***-****. -> Option A
Quick Check:
Digits replaced by * = Call me at ***-***-****. [OK]
Hint: Digits \d replaced by * everywhere in string [OK]
A. Regex is case-sensitive, so 'world' does not match 'World'.
B. Missing replacement string in gsub.
C. Syntax error due to missing parentheses.
D. gsub cannot be used with regex.
Solution
Step 1: Check regex matching
The regex /world/ is lowercase and Ruby regex is case-sensitive by default.
Step 2: Compare with string content
The string has "World" with uppercase W, so the regex does not match anything.
Final Answer:
Regex is case-sensitive, so 'world' does not match 'World'. -> Option A
Quick Check:
Case sensitivity causes no match [OK]
Hint: 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
5. Given the string data = "User: John_Doe, Email: john@example.com", which gsub call will replace all non-alphanumeric characters (except underscore) with a dash "-"?
hard
A. data.gsub(/[\w]/, "-")
B. data.gsub(/\w/, "-")
C. data.gsub(/[^a-zA-Z0-9]/, "-")
D. data.gsub(/[^a-zA-Z0-9_]/, "-")
Solution
Step 1: Understand character classes
\w matches letters, digits, and underscore. \W matches anything not in \w.