0
0
Rubyprogramming~10 mins

Gsub with regex in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gsub with regex
Start with original string
Apply gsub with regex
Match regex pattern in string?
NoReturn modified string
Yes
Replace matched part
Continue to next match
Back to Match regex pattern
The program starts with a string, applies gsub with a regex to find matches, replaces each match, and continues until no matches remain, then returns the new string.
Execution Sample
Ruby
text = "hello 123 world 456"
new_text = text.gsub(/\d+/, "#")
puts new_text
This code replaces all groups of digits in the string with the '#' character.
Execution Table
StepCurrent StringRegex MatchReplacementResulting String
1hello 123 world 456/\d+/"#"hello # world 456
2hello # world 456/\d+/"#"hello # world #
3hello # world #/\d+/nonehello # world #
💡 No more regex matches found, gsub returns the final modified string.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
text"hello 123 world 456""hello 123 world 456""hello 123 world 456""hello 123 world 456"
new_textnil"hello # world 456""hello # world #""hello # world #"
Key Moments - 3 Insights
Why does gsub replace all matches, not just the first one?
Because gsub scans the entire string for all matches of the regex, replacing each one, as shown in execution_table steps 1 and 2.
What happens if the regex does not match anything in the string?
gsub returns the original string unchanged, as shown in execution_table step 3 where no match is found.
Why is the original string 'text' unchanged after gsub?
Because gsub returns a new string with replacements; it does not modify the original string in place.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of new_text after step 2?
A"hello # world 456"
B"hello # world #"
C"hello 123 world 456"
D"hello world 456"
💡 Hint
Check the 'Resulting String' column in row for step 2.
At which step does gsub find no more matches?
AStep 3
BStep 2
CStep 1
DNever
💡 Hint
Look at the 'Regex Match' column where it says 'none'.
If the regex was changed to /world/, what would be the first replacement?
A"hello # 123 # 456"
B"654 # 321 # olleh"
C"hello 123 # 456"
Dhello # 123 # 456"
💡 Hint
Consider that only the word 'world' would be replaced with '#', not digits.
Concept Snapshot
gsub with regex:
- Syntax: string.gsub(/pattern/, replacement)
- Finds all matches of regex pattern in string
- Replaces each match with replacement
- Returns new string, original unchanged
- Useful for replacing patterns like digits, words, etc.
Full Transcript
This visual execution shows how Ruby's gsub method works with a regex pattern. Starting with the string 'hello 123 world 456', gsub searches for all digit groups using /\d+/. At step 1, it finds '123' and replaces it with '#', resulting in 'hello # world 456'. At step 2, it finds '456' and replaces it with '#', resulting in 'hello # world #'. At step 3, no more matches are found, so gsub returns the final string. The original string remains unchanged. This helps beginners see how gsub scans and replaces all matches step-by-step.