Frozen objects in Ruby - Time & Space Complexity
When working with frozen objects in Ruby, it's important to understand how checking if an object is frozen affects performance.
We want to know how the time to check freezing grows as the program runs.
Analyze the time complexity of the following code snippet.
array = ["apple", "banana", "cherry"].map(&:freeze)
array.each do |item|
puts item.frozen?
end
This code freezes each string in an array and then checks if each item is frozen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the array to check if it is frozen.
- How many times: Once for each item in the array (n times).
Each item is checked once, so the total work grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work increases evenly as the list gets bigger.
Time Complexity: O(n)
This means the time to check frozen status grows in a straight line with the number of items.
[X] Wrong: "Checking if an object is frozen takes a long time because it inspects the whole object deeply."
[OK] Correct: Checking frozen status is a simple flag check and does not depend on object size.
Understanding how simple property checks scale helps you reason about performance in real code, making you confident in explaining your choices.
"What if we froze a nested array inside each item and checked frozen? How would the time complexity change?"
Practice
freeze method do to an object in Ruby?Solution
Step 1: Understand the purpose of
Thefreezefreezemethod is used to make an object immutable, meaning it cannot be changed after freezing.Step 2: Identify the effect on the object
Once frozen, any attempt to modify the object will raise an error, so it prevents modifications.Final Answer:
Prevents any modifications to the object -> Option DQuick Check:
freeze makes object immutable = Prevents modifications [OK]
- Thinking freeze deletes the object
- Confusing freeze with duplication
- Believing freeze makes object mutable
str in Ruby?Solution
Step 1: Recall Ruby method call syntax
In Ruby, methods are called on objects using dot notation, e.g.,object.method.Step 2: Identify the correct freeze syntax
The correct way to freeze an object is by callingfreezeon it:str.freeze(). The other options are invalid syntax.Final Answer:
str.freeze() -> Option AQuick Check:
Method call syntax = str.freeze() [OK]
- Using freeze as a standalone keyword
- Assigning freeze as a property
- Calling freeze like a function without dot
arr = [1, 2, 3] arr.freeze arr << 4 puts arr.inspect
Solution
Step 1: Understand freezing the array
The arrayarris frozen witharr.freeze, so it cannot be changed.Step 2: Analyze the modification attempt
The code tries to add an element witharr << 4, which modifies the array. Since it's frozen, this raises a runtime error.Final Answer:
RuntimeError: can't modify frozen Array -> Option AQuick Check:
Modifying frozen object = RuntimeError [OK]
- Expecting the array to change
- Thinking freeze allows modifications
- Confusing runtime error with syntax error
name = "Alice" name.freeze name[0] = 'M' puts name
Solution
Step 1: Recognize string freezing
The stringnameis frozen, so it cannot be changed aftername.freeze.Step 2: Understand the attempted modification
Trying to change the first character withname[0] = 'M'modifies the string, which is forbidden and raises a runtime error.Final Answer:
RuntimeError: can't modify frozen String -> Option BQuick Check:
Changing frozen string = RuntimeError [OK]
- Assuming strings can be changed after freeze
- Confusing syntax error with runtime error
- Ignoring freeze effect on string elements
hash = {a: [1, 2], b: [3, 4]}
hash.freeze
hash[:a] << 5
puts hash[:a].inspect
What does this tell us about freezing objects?Solution
Step 1: Understand freezing the hash
The hash object is frozen, so its structure (keys and values) cannot be changed directly.Step 2: Analyze modification of nested array
The value for key:ais an array, which is not frozen. Modifying this array with<< 5is allowed.Step 3: Observe the output
The array now contains [1, 2, 5], showing that freezing the hash does not freeze nested objects.Final Answer:
[1, 2, 5] - freeze prevents changing the hash keys but not the objects inside -> Option CQuick Check:
Freeze is shallow = nested objects can change [OK]
- Assuming freeze affects nested objects
- Expecting runtime error on nested modification
- Confusing hash freezing with deep freeze
Ready to prove it?
Pass the test and this pattern moves unknown
