DEV Community

[Comment from a deleted post]
 
philnash profile image
Phil Nash

I think I can see why this is only the case for string literals. Firstly, string literals get used all over the place, in constants and as hash keys particularly, and those gain the most benefit from being frozen. However, given the existence and wide usage of Ruby's mutable strings, making all strings immutable would be a huge breaking change (even this is a breaking change, but the magic comment does allow us to test and get into it gently before Ruby 3). It also means we continue to have easy ways to create a mutable string, using String.new or "string".dup.

Don't forget, if you create a string another way and want it to be frozen, you can still call freeze on it and gain the same benefits.