# frozen_string_literal

If you are coding in Ruby 2.3 or newer, using RuboCop, you may have noticed this warning:

What is this? To improve performance, there were plans in Ruby 3 to use frozen string literals by default. In the end, this feature implementation was dropped. However, you can still use this magic comment, which must appear on the first line in the file, to declare your intention:

# frozen_string_literal: true

Since Ruby 2.3 if you run with –enable=frozen-string-literal all string literals are frozen by default. You can override this setting in a file with the same magic comment:

# frozen_string_literal: false

If you are not opting to use frozen string literals by default , you can still manually freeze them:

GREETING = "welcome".freeze

# if you try to modify the string, you will get a runtime error:
# can't modify frozen String: "welcome" (FrozenError)

GREETING.prepend("hi, ")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s