Any object may be frozen by calling its freeze
method.
A frozen object becomes immutable: none of its internal state may be changed, and an attempt to call any of its mutator methods fails:
s = "ice" # Strings are mutable objects s.freeze # Make this string immutable s.frozen? # true: it has been frozen s.upcase! # TypeError: can't modify frozen string s[0] = "ni" # TypeError: can't modify frozen string
Freezing a class object prevents the addition of any methods to that class.
You can check whether an object is frozen with the frozen?
method.
Once frozen, there is no way to thaw
an object.
If you copy a frozen object with clone
, the copy will also be frozen.
If you copy a
frozen object with dup
, however, the copy will not be frozen.
Casiano Rodriguez León 2015-01-07