📖 Scenario: You are working on a simple Ruby program that manages a list of favorite fruits. To avoid accidental changes to this list later in the program, you want to make the list immutable.
🎯 Goal: Create a frozen array of fruits so that the list cannot be changed after creation. Then, try to add a new fruit to see what happens.
📋 What You'll Learn
Create an array called fruits with the exact values 'apple', 'banana', and 'cherry'
Create a variable called immutable_fruits that holds the frozen version of fruits
Attempt to add 'orange' to immutable_fruits using the push method
Print the immutable_fruits array
💡 Why This Matters
🌍 Real World
Freezing data helps prevent accidental changes in programs, which is important in many real-world applications like configuration settings or constant values.
💼 Career
Understanding immutability and error handling is useful for writing safer, more reliable Ruby code in software development jobs.
Progress0 / 4 steps
1
Create the initial array of fruits
Create an array called fruits with the exact values 'apple', 'banana', and 'cherry'.
Ruby
Hint
Use square brackets [] to create an array and separate items with commas.
2
Freeze the array to make it immutable
Create a variable called immutable_fruits that holds the frozen version of the fruits array using the freeze method.
Ruby
Hint
Use freeze method on the array to make it immutable.
3
Try to add a new fruit to the frozen array
Use the push method to try to add 'orange' to the immutable_fruits array.
Ruby
Hint
Use push method on immutable_fruits. Wrap it in begin...rescue to catch the error.
4
Print the frozen array and error message
Print the immutable_fruits array and then print the error_message variable to show the error caused by trying to modify the frozen array.
Ruby
Hint
Use puts to print the array and the error message. Use inspect to show the array nicely.
Practice
(1/5)
1. What does the freeze method do to an object in Ruby?
easy
A. Prevents any further modifications to the object
B. Deletes the object from memory
C. Duplicates the object
D. Makes the object mutable
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 further changes.
Final Answer:
Prevents any further modifications to the object -> Option A
Quick Check:
freeze makes object immutable = Prevents changes [OK]
Hint: Freeze means no changes allowed after calling it [OK]
Common Mistakes:
Thinking freeze deletes the object
Confusing freeze with duplication
Assuming freeze makes object mutable
2. Which of the following is the correct way to freeze a string name in Ruby?
easy
A. freeze(name)
B. name.frozen = true
C. name.freeze()
D. freeze name
Solution
Step 1: Recall Ruby method call syntax
In Ruby, to call a method on an object, use dot notation like object.method().
Step 2: Identify the correct freeze syntax
The correct way to freeze the string name is name.freeze(). Other options are invalid syntax or incorrect usage.
Final Answer:
name.freeze() -> Option C
Quick Check:
Method call with dot and parentheses = Correct freeze syntax [OK]
Hint: Use dot notation: object.freeze() to freeze [OK]