Enforced immutability: Difference between revisions

Content added Content deleted
m (variable trait "is readonly" is not valid anymore)
(→‎{{header|Ruby}}: Added comment about freezing strings in Ruby > 2.0)
Line 817: Line 817:
msg2 = msg
msg2 = msg


# An object is frozen and it is not a variable.
# The object is frozen, not the variable.
msg = "hello world" # A new object was assigned to the variable.
msg = "hello world" # A new object was assigned to the variable.


puts msg.frozen? #=> false
puts msg.frozen? #=> false
puts msg2.frozen? #=> true</lang>
puts msg2.frozen? #=> true</lang>
Since Ruby version 2.1 freezing strings can give a performance boost.
There is no way to unfreeze a frozen object.
There is no way to unfreeze a frozen object.
The freeze can not be canceled but the object of approximately the same contents not to freeze up can be gotten if using Object#dup.
The freeze can not be canceled but the object of approximately the same contents not to freeze up can be gotten if using Object#dup.