Concept Flow - Frozen objects
Create object
Freeze object
Try to modify object?
Yes→Error: can't modify frozen object
No
Use object as is
End
An object is created and then frozen to prevent changes. Any attempt to modify it causes an error.
str = "hello" str.freeze str << " world"
| Step | Action | Object State | Result/Output |
|---|---|---|---|
| 1 | Create string str = "hello" | str = "hello" (not frozen) | No output |
| 2 | Call str.freeze | str = "hello" (frozen) | No output |
| 3 | Try str << " world" | str frozen | Error: can't modify frozen String |
| Variable | Start | After freeze | After modification attempt |
|---|---|---|---|
| str | "hello" (not frozen) | "hello" (frozen) | "hello" (frozen, unchanged) |
In Ruby, calling freeze on an object makes it immutable. Any attempt to change a frozen object raises an error. Use freeze to protect objects from accidental modification. Frozen objects keep their content but cannot be altered. Trying to modify frozen objects stops the program with an error.