Enforced immutability: Difference between revisions
→{{header|Ruby}}: Added comment about freezing strings in Ruby > 2.0
m (variable trait "is readonly" is not valid anymore) |
(→{{header|Ruby}}: Added comment about freezing strings in Ruby > 2.0) |
||
Line 817:
msg2 = msg
#
msg = "hello world" # A new object was assigned to the variable.
puts msg.frozen? #=> false
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.
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.
|