0
0
Rubyprogramming~10 mins

String methods (upcase, downcase, strip) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String methods (upcase, downcase, strip)
Start with original string
Apply upcase method
String in all uppercase
Apply downcase method
String in all lowercase
Apply strip method
String with no leading/trailing spaces
End
The string starts as is, then you can change it to all uppercase, all lowercase, or remove spaces at the start and end.
Execution Sample
Ruby
str = "  Hello World  "
puts str.upcase
puts str.downcase
puts str.strip
This code shows how to change a string to uppercase, lowercase, and remove spaces around it.
Execution Table
StepCode LineOperationString ValueOutput
1str = " Hello World "Assign original string" Hello World "
2puts str.upcaseConvert to uppercase" HELLO WORLD " HELLO WORLD
3puts str.downcaseConvert to lowercase" hello world " hello world
4puts str.stripRemove spaces at start/end"Hello World"Hello World
💡 All string methods applied and printed; program ends.
Variable Tracker
VariableStartAfter upcaseAfter downcaseAfter strip
str" Hello World "" Hello World "" Hello World "" Hello World "
Key Moments - 2 Insights
Why does the original string 'str' not change after calling upcase, downcase, or strip?
Because these methods return a new string and do not modify the original string 'str'. This is shown in the variable_tracker where 'str' stays the same after each method call.
What does the strip method remove from the string?
It removes only the spaces at the start and end of the string, not spaces between words. This is clear in the execution_table step 4 where the output has no spaces around but the space between 'Hello' and 'World' remains.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the output of str.upcase?
A HELLO WORLD
Bhello world
CHello World
DDLROW OLLEH
💡 Hint
Check the 'Output' column in execution_table row 2.
According to variable_tracker, what is the value of 'str' after calling strip?
A"Hello World"
B"hello world"
C" Hello World "
D"HELLO WORLD"
💡 Hint
Look at the 'After strip' column for 'str' in variable_tracker.
At which step does the string lose its leading and trailing spaces?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Check the 'Operation' and 'Output' columns in execution_table to see when spaces are removed.
Concept Snapshot
String methods in Ruby:
- upcase: returns string in all uppercase
- downcase: returns string in all lowercase
- strip: removes spaces at start and end
Original string stays unchanged unless reassigned
Use puts to print results
Full Transcript
We start with a string that has spaces around it. We assign it to variable str. Then we call str.upcase which returns a new string with all letters uppercase but does not change str itself. Next, str.downcase returns a new string with all letters lowercase, again str stays the same. Finally, str.strip returns a new string with spaces removed from the start and end. The original string str remains unchanged throughout. Each method returns a new string which we print using puts.