Freezing a string stops it from being changed. This helps keep data safe and avoids mistakes.
String freezing for immutability in Ruby
Introduction
Syntax
Ruby
string.freezeCalling freeze on a string makes it immutable (unchangeable).
Trying to change a frozen string will cause an error.
Examples
name.Ruby
name = "hello" name.freeze
"world" directly.Ruby
"world".freeze
frozen? before and after freezing.Ruby
greeting = "hi" puts greeting.frozen? # Output: false greeting.freeze puts greeting.frozen? # Output: true
Sample Program
This program shows how freezing a string works and what happens if you try to change it after freezing.
Ruby
greeting = "hello" puts greeting.frozen? # false greeting.freeze puts greeting.frozen? # true # Trying to change frozen string causes error begin greeting << " world" rescue => e puts e.message end
Important Notes
Freezing strings is useful to avoid bugs caused by accidental changes.
Frozen strings can improve memory usage by reusing the same object.
In Ruby 3.0+, you can use the magic comment # frozen_string_literal: true to freeze all string literals in a file.
Summary
Freezing a string makes it unchangeable.
Trying to modify a frozen string causes an error.
Use freezing to protect important string data and improve performance.
Practice
1. What does freezing a string in Ruby do?
str = "hello".freezeeasy
Solution
Step 1: Understand the freeze method
Thefreezemethod in Ruby makes an object immutable, meaning it cannot be changed after freezing.Step 2: Apply to strings
When applied to a string, it prevents any further modifications to that string's content.Final Answer:
Makes the string unchangeable (immutable). -> Option CQuick Check:
freeze means immutable = A [OK]
Hint: Freeze means no changes allowed to the string [OK]
Common Mistakes:
- Thinking freeze deletes the string
- Assuming freeze changes string content
- Believing freeze speeds up modification
2. Which of the following is the correct way to freeze a string in Ruby?
easy
Solution
Step 1: Review Ruby freeze syntax
The correct syntax to freeze a string is to call.freezeon the string object, like"hello".freeze.Step 2: Check each option
str = "hello".freeze correctly calls.freezeon the string literal. Options B, C, and D use invalid syntax or incorrect method calls.Final Answer:
str = "hello".freeze -> Option BQuick Check:
Call .freeze on string = A [OK]
Hint: Use .freeze after the string literal [OK]
Common Mistakes:
- Trying to assign freeze as a variable
- Using freeze as a standalone keyword
- Calling freeze as a function with parentheses
3. What will happen when the following Ruby code runs?
str = "hello".freeze str << " world"
medium
Solution
Step 1: Understand string freezing effect
Freezing the string"hello"makes it immutable, so any modification attempt will raise an error.Step 2: Analyze the << operation
The<<operator tries to append " world" to the frozen string, which is not allowed and causes a runtime error.Final Answer:
A runtime error occurs: can't modify frozen String. -> Option DQuick Check:
Modifying frozen string = error C [OK]
Hint: Appending to frozen string causes error [OK]
Common Mistakes:
- Assuming string changes silently
- Thinking string resets to new value
- Believing no error occurs
4. Identify the error in this Ruby code:
name = "Alice" name.freeze name.upcase!
medium
Solution
Step 1: Check freeze usage
Thefreezemethod is correctly called on the stringname, making it immutable.Step 2: Understand upcase! effect
Theupcase!method tries to change the string in place, which is not allowed on a frozen string, causing a runtime error.Final Answer:
Error because upcase! tries to modify a frozen string. -> Option AQuick Check:
Modifying frozen string with upcase! = error B [OK]
Hint: In-place methods fail on frozen strings [OK]
Common Mistakes:
- Thinking freeze is called incorrectly
- Assuming no error on upcase!
- Confusing variable type
5. You want to store a constant greeting message that should never change in your Ruby program. Which approach is best?
Why is freezing important here?
GREETING = "Welcome!".freeze
Why is freezing important here?
hard
Solution
Step 1: Understand constant usage
Constants likeGREETINGshould not change during program execution to avoid bugs.Step 2: Role of freeze on constants
Freezing the string ensures it cannot be modified accidentally, protecting the constant's value.Final Answer:
It prevents accidental changes to the greeting, ensuring data integrity. -> Option AQuick Check:
Freeze constant for safety = D [OK]
Hint: Freeze constants to keep them safe from changes [OK]
Common Mistakes:
- Thinking freeze speeds up printing
- Believing freeze allows changes later
- Assuming freeze compresses data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
