hi,
sometimes we want our object or instance variables should be immutable (none
changeable).
for example, if you have the following code -
data = ["a", "b"]
call_some_method(data)
you expect "call_some_method" shouldn't change your argument. there are two
way you can ensure immutability.
1. clone your array while you passing through the method.
call_some_method(data.clone)
2. freeze your array so none can change your array.
data.freeze
call_some_method(data)
in this case, if someone try to change the value of data it will throw an
exception.
similarly we can ensure mutability for classes as well, as you know ruby as
great advantage of being open. anytime anywhere you can replace or change
the class behavior.
for example:
class String
def print
puts self
end
end
so sometimes your exposed api should be more preventive, in that case don't
forget to use freeze.
for example - if String class was freezed you won't be able to modify this
class anymore.
hope this will help to understand where should you use freeze.
best wishes,
--
----------------------------------------------------
nhm tanveer hossain khan (hasan)
http://hasan.we4tech.com
----------------------------------------------------
mobile: +880 1713 090 511
----------------------------------------------------
"work for fun"
[Non-text portions of this message have been removed]