Why strings are mutable in Ruby - Performance Analysis
We want to understand how changing strings in Ruby affects the time it takes to run code.
Specifically, we ask: How does the ability to change strings impact the number of steps the program takes?
Analyze the time complexity of modifying a string repeatedly.
str = "hello"
10.times do |i|
str << "!"
end
puts str
This code adds an exclamation mark to the string 10 times, changing the string each time.
Look for repeated actions that take time.
- Primary operation: Adding a character to the string with << inside a loop.
- How many times: 10 times, once per loop cycle.
As we add more characters, the work grows with the number of additions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The time grows roughly in direct proportion to how many times we add to the string.
Time Complexity: O(n)
This means if you add to the string n times, the time it takes grows roughly the same as n.
[X] Wrong: "Adding to a string always takes the same time no matter how long it is."
[OK] Correct: Actually, adding to a string can take longer if the string needs to grow its storage space, so time depends on how many times you add.
Understanding how string changes affect time helps you write clear and efficient code, a skill valued in many coding challenges and real projects.
"What if we used string concatenation with + instead of << inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand mutability concept in Ruby strings
Ruby strings can be changed after creation, meaning their content can be updated without making a new string object.Step 2: Identify why this is useful
This allows efficient text updates and memory use, as you don't need to create new strings for every change.Final Answer:
Because Ruby allows changing string content without creating new objects -> Option AQuick Check:
Ruby strings are mutable = Because Ruby allows changing string content without creating new objects [OK]
- Confusing mutability with immutability
- Thinking strings are stored as numbers
- Believing Ruby strings cannot be changed
Solution
Step 1: Check each code for mutability
str = "hello"; str[0] = 'H' changes the first character directly, showing string mutability.Step 2: Compare other options
str = "hello"; str = str + '!' creates a new string; str = "hello"; str.upcase returns a new uppercase string without changing the original; str = "hello"; str.freeze freezes string making it immutable.Final Answer:
str = "hello"; str[0] = 'H' -> Option BQuick Check:
Direct index assignment changes string = str = "hello"; str[0] = 'H' [OK]
- Using freeze which prevents changes
- Thinking concatenation changes original string
- Confusing upcase method with direct assignment
str = "cat" str[1] = 'o' puts str
Solution
Step 1: Understand string indexing and assignment
str[1] = 'o' changes the character at index 1 (second character) from 'a' to 'o'.Step 2: Apply the change and print
Original string "cat" becomes "cot" after the change, so puts prints "cot".Final Answer:
cot -> Option CQuick Check:
Changing index 1 to 'o' = cot [OK]
- Expecting original string unchanged
- Confusing index positions starting at 1
- Thinking it causes an error
str = "dog" str.freeze str[0] = 'c' puts str
Solution
Step 1: Understand freeze method effect
freeze makes the string immutable, so any modification causes an error.Step 2: Identify error on modification attempt
str[0] = 'c' tries to change the frozen string, causing a runtime error "can't modify frozen String".Final Answer:
Cannot modify frozen string error -> Option AQuick Check:
freeze prevents changes = error [OK]
- Ignoring freeze effect
- Expecting string to change after freeze
- Thinking freeze causes syntax error
str = "hello" # Update 'e' and 'o' to 'a' and 'u'
Solution
Step 1: Identify direct string mutation
str[1] = 'a'; str[4] = 'u' changes characters by index directly, using mutability efficiently without creating new strings.Step 2: Compare other options
str = str.sub('e', 'a').sub('o', 'u') creates new strings with sub; str = "hallu" assigns a new string; str.freeze; str[1] = 'a' freezes string then tries to change causing error.Final Answer:
str[1] = 'a'; str[4] = 'u' -> Option DQuick Check:
Direct index assignment updates string efficiently [OK]
- Using sub which creates new strings
- Assigning new string instead of mutating
- Freezing string before modification
Ready to prove it?
Pass the test and this pattern moves unknown
