0
0
Rubyprogramming~10 mins

Gsub and sub for replacement in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gsub and sub for replacement
Start with original string
Call sub or gsub method
Check for first match
Replace first match
For gsub: Continue checking for more matches
Replace all matches
Return modified string
The program starts with a string, then uses sub to replace the first match or gsub to replace all matches, returning the new string.
Execution Sample
Ruby
text = "hello hello"
new_text_sub = text.sub("hello", "hi")
new_text_gsub = text.gsub("hello", "hi")
puts new_text_sub
puts new_text_gsub
This code replaces the first 'hello' with 'hi' using sub, and all 'hello's with 'hi' using gsub, then prints both results.
Execution Table
StepMethod CalledString BeforeMatch Found?ActionString AfterOutput
1subhello helloYes (first 'hello')Replace first 'hello' with 'hi'hi hello
2puts new_text_subhi helloN/APrint stringhi hellohi hello
3gsubhello helloYes (all 'hello's)Replace all 'hello' with 'hi'hi hi
4puts new_text_gsubhi hiN/APrint stringhi hihi hi
5EndN/AN/ANo more actionsN/AExecution stops
💡 All replacements done and outputs printed, program ends.
Variable Tracker
VariableStartAfter subAfter gsubFinal
text"hello hello""hello hello""hello hello""hello hello"
new_text_subnil"hi hello""hi hello""hi hello"
new_text_gsubnilnil"hi hi""hi hi"
Key Moments - 2 Insights
Why does sub only replace the first occurrence but gsub replaces all?
Because sub is designed to replace only the first match it finds (see execution_table step 1), while gsub continues to find and replace all matches (see step 3).
Does the original string change after sub or gsub?
No, the original string 'text' stays the same throughout (see variable_tracker), because sub and gsub return new strings without modifying the original.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the string after calling sub?
A"hi hello"
B"hello hi"
C"hi hi"
D"hello hello"
💡 Hint
Check the 'String After' column in execution_table row for step 1.
At which step does gsub replace all occurrences?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the row where Method Called is 'gsub' and action mentions replacing all matches.
If we used sub instead of gsub in step 3, what would be the output at step 4?
A"hello hello"
B"hi hello"
C"hi hi"
D"hello hi"
💡 Hint
Recall sub replaces only the first match, so only the first 'hello' changes.
Concept Snapshot
sub and gsub replace text in strings.
sub replaces only the first match.
gsub replaces all matches.
Both return new strings; original stays unchanged.
Use sub for single replacement, gsub for global.
Full Transcript
This visual execution shows how Ruby's sub and gsub methods work for replacing text in strings. Starting with the string 'hello hello', sub replaces only the first 'hello' with 'hi', resulting in 'hi hello'. Then gsub replaces all 'hello' occurrences with 'hi', resulting in 'hi hi'. The original string remains unchanged throughout. The execution table tracks each step, showing method calls, matches found, actions taken, and resulting strings. Variable tracking confirms that the original string 'text' does not change, while new variables hold the replaced strings. Key moments clarify why sub replaces only the first match and gsub replaces all, and that original strings are not modified. The quiz tests understanding of these steps and outcomes.