How can I describe mutable strings when strings are by default immutable?

When the file has a pragma:

# frozen_string_literal: true 

all lines written as literals in this file are frozen by default. When I want my lines to be immutable in general, and therefore I use pragma, but want to have a couple of mutable lines, what is the recommended way to write them?

All I can think of is:

 String.new("foo") 
+7
string ruby literals
source share
2 answers

I missed it. The recommended method is to use the string literal of the +@ method.

 (+"foo").frozen? # => false (-"foo").frozen? # => true "foo".frozen? # => true 
+7
source share

You can dup literal to make it mutable:

 "foo".dup.frozen? # => false 
+4
source share

All Articles