String methods (upcase, downcase, strip) in Ruby - Time & Space Complexity
We want to understand how long it takes for string methods like upcase, downcase, and strip to finish as the string gets longer.
How does the time needed change when the string size grows?
Analyze the time complexity of the following code snippet.
str = " Hello World "
up = str.upcase
down = str.downcase
clean = str.strip
This code changes the string to all uppercase, all lowercase, and removes spaces from the start and end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each method goes through the string characters one by one.
- How many times: Once for each character in the string.
As the string gets longer, the time to change case or remove spaces grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows evenly with the string length.
Time Complexity: O(n)
This means the time to run these methods grows directly with the string size.
[X] Wrong: "These methods run instantly no matter how long the string is."
[OK] Correct: Each character must be checked or changed, so longer strings take more time.
Knowing how string methods scale helps you explain your code's speed clearly and shows you understand how computers handle text.
"What if we used a method that only changed the first character? How would the time complexity change?"
Practice
upcase do to a string?Solution
Step 1: Understand the method name
The methodupcasesuggests making letters uppercase.Step 2: Recall the method effect on strings
upcasechanges all letters to uppercase, not lowercase or trimming spaces.Final Answer:
Converts all letters in the string to uppercase -> Option BQuick Check:
upcase = uppercase letters [OK]
- Confusing upcase with downcase
- Thinking upcase removes spaces
- Assuming upcase reverses the string
str in Ruby?Solution
Step 1: Identify the method to remove spaces
The Ruby method to remove spaces at start and end isstrip, called on the string object.Step 2: Check correct method call syntax
In Ruby, methods are called with dot notation:str.strip(). There is notrimmethod in Ruby.Final Answer:
str.strip() -> Option AQuick Check:
strip method called with dot = str.strip() [OK]
- Using strip as a standalone function
- Using trim instead of strip
- Calling upcase to remove spaces
str = " Hello World " puts str.strip.upcase
Solution
Step 1: Apply
stripto the stringstripremoves spaces at the start and end, so " Hello World " becomes "Hello World".Step 2: Apply
upcaseto the stripped stringupcaseconverts all letters to uppercase, so "Hello World" becomes "HELLO WORLD".Final Answer:
"HELLO WORLD" -> Option DQuick Check:
strip + upcase = "HELLO WORLD" [OK]
- Forgetting strip removes spaces
- Applying upcase before strip
- Expecting original spaces to remain
name to lowercase and remove spaces at the start and end. What is wrong?name = " RubyLang " name.downcase.strip
Solution
Step 1: Check method calls and order
Bothdowncaseandstripare valid methods and can be chained in any order.Step 2: Check if result is used
The code calls methods but does not save or print the result, so the original string remains unchanged.Final Answer:
The result is not saved or printed -> Option CQuick Check:
Method calls without assignment or output do nothing visible [OK]
- Thinking method order matters here
- Assuming methods change string in place
- Confusing strip with a non-existent method
names = [" Alice", "BOB ", " cHaRlie "]
Which Ruby code correctly cleans this list by removing spaces and making all names lowercase?
Solution
Step 1: Understand the goal
We want to remove spaces and convert all names to lowercase.Step 2: Check each option
clean_names = names.map { |n| n.strip.downcase } usesstripthendowncase, which removes spaces first, then lowercases. clean_names = names.map { |n| n.downcase.chomp } uses chomp after downcase, but chomp removes only trailing newlines, not spaces, so spaces remain. clean_names = names.map { |n| n.strip.upcase } usesupcasewhich is wrong. clean_names = names.map { |n| n.downcase } does not remove spaces.Step 3: Choose best practice
The code that usesstripto remove whitespace anddowncaseto lowercase correctly cleans the names.Final Answer:
clean_names = names.map { |n| n.strip.downcase } -> Option AQuick Check:
strip then downcase cleans names correctly [OK]
- Using upcase instead of downcase
- Confusing chomp with strip
- Forgetting to map over the list
Ready to prove it?
Pass the test and this pattern moves unknown
