What is “frozen_string_literal” in Ruby?

What is “frozen_string_literal” in Ruby?

ruby3

Freezing Strings feature improves apps performance by freezing Strings. So, Matz – Ruby’s creator – decided to make all String literals frozen (immutable) by default in Ruby 3.0.

In order to have a transition path to this coming big change, it was decided to have a magic comment at the beginning of files, so you can use in Ruby 2.3.

To do so, just add this comment in the first line of your files:

# frozen_string_literal: true

class YourClass
  # ...
end

Trying adding it to your spec_helper file and let me know in the comments if you performance improved!

More info: Ruby issue #8976.

Comments are closed.