📖 Scenario: Imagine you are managing a list of favorite fruits in a program. Sometimes, you want to make sure this list does not change accidentally after you set it.
🎯 Goal: You will create a list of fruits, freeze it to prevent changes, and then try to modify it to see what happens.
📋 What You'll Learn
Create an array called fruits with the exact values: 'apple', 'banana', 'cherry'
Create a variable called frozen_fruits that is the frozen version of fruits
Attempt to add 'orange' to frozen_fruits using push
Print frozen_fruits to show the result
💡 Why This Matters
🌍 Real World
Freezing objects is useful when you want to keep data safe and unchanged, like configuration settings or fixed lists.
💼 Career
Understanding frozen objects helps you write safer Ruby code, which is important for roles like Ruby developer or backend engineer.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact strings: 'apple', 'banana', 'cherry'
Ruby
Hint
Use square brackets [] to create an array with the given strings separated by commas.
2
Freeze the fruits array
Create a variable called frozen_fruits and set it to the frozen version of the fruits array using the freeze method
Ruby
Hint
Call freeze on fruits and assign it to frozen_fruits.
3
Try to modify the frozen array
Use the push method to add the string 'orange' to the frozen_fruits array
Ruby
Hint
Call push('orange') on frozen_fruits to try adding an item.
4
Print the frozen array
Write a puts statement to print the frozen_fruits array
Ruby
Hint
Use puts frozen_fruits to print each fruit on its own line.
Note: Since the array is frozen, adding 'orange' will cause an error and the program will stop before printing.
Practice
(1/5)
1. What does the freeze method do to an object in Ruby?
easy
A. Makes the object mutable
B. Deletes the object from memory
C. Duplicates the object
D. Prevents any modifications to the object
Solution
Step 1: Understand the purpose of freeze
The freeze method 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 D
Quick Check:
freeze makes object immutable = Prevents modifications [OK]
Hint: Freeze means no changes allowed to the object [OK]
Common Mistakes:
Thinking freeze deletes the object
Confusing freeze with duplication
Believing freeze makes object mutable
2. Which of the following is the correct way to freeze a string object str in Ruby?
easy
A. str.freeze()
B. freeze str
C. str.freeze = true
D. freeze(str)
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 calling freeze on it: str.freeze(). The other options are invalid syntax.
Final Answer:
str.freeze() -> Option A
Quick Check:
Method call syntax = str.freeze() [OK]
Hint: Use dot notation to call freeze: object.freeze() [OK]