Issue #8976 has been updated by Akira Matsuda.


Let us bring this two-year-aged very well matured feature proposal back to the table!

I would like to propose again to put this magic comment for freezing String literals into next version of Ruby.

## motivation
There are several reasons that we need this feature **now**:

(1) Freezing Strings is getting people's attraction recently, because people have been started realizing that they can somewhat improve their app performance by freezing Strings.
Hence, we see so many "performance improvement" patches that add `.freeze` to String literals here and there for [frameworks](https://github.com/rails/rails/commit/5bb1d4d288d019e276335465d0389fd2f5246bfd), [libraries](https://github.com/rails/sprockets/commit/8fc09576d21e37a51fd12284844c35d826c91c02), and even for [ruby stdlibs](https://github.com/ruby/ruby/pull/996).
These "performance improvement".freeze patches would certainly improve some micro-benchmark results, but at the same time inject code ugliness in exchange for the speed.
This apparently is a wrong approach. This is not a Ruby way. Ruby code must be kept beautiful.

(2) While discussing this freezing magic comment feature, Matz finally decided to make all String literals frozen by default (#11473).
Having a magic comment switch per file would be a very good transition path to the coming big change in Ruby 3.

## the magic comment

We chose this at the meeting.

~~~
# frozen_string_literal: true
~~~

## command line option

A command line option switch for "ruby 3 mode" will also be added.
If the option and the magic comment conflicts, the magic comment should win.

## creating a String with an Encoding

While investigating ruby code in the stdlib, we found that in most cases mutating literal Strings happens in this form

~~~
"".force_encoding(Encoding::ASCII_8BIT)
~~~

so we decided to introduce a new String costructor taking Encoding via kwarg as follows:

~~~
String.new("", encoding: Encoding::ASCII_8BIT)
~~~

## current status

Matz already accepted this proposal at the developer meeting in Tokyo yesterday, so this feature will be put into 2.3 unless we see any strong objection here.


----------------------------------------
Feature #8976: file-scope freeze_string directive
https://bugs.ruby-lang.org/issues/8976#change-53900

* Author: Akira Tanaka
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Yesterday, we had a face-to-face developer meeting.
https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20131001Japan
Several committers attended.
matz didn't attended, though.  (This means this issue is not concluded.)

We believe we found a better way to freeze static string literals for
less GC pressure.
"static string literal" is a string literal without dynamic expression.

Currently, f-suffix, "..."f, is used to freeze a string literal to avoid
String object allocation.

There are several problems for f-suffix:

* The notation is ugly.
* Syntax error on Ruby 2.0.
  We cannot use the feature in version independent libraries.
  So, it is difficult to deploy.
* Need to modify for each string literal.
  This is cumbersome.

The new way we found is a file-scope directive as follows

  # freeze_string: true

The above comment at top of a file changes semantics of
static string literals in the file.
The static string literals will be frozen and always returns same object.
(The semantics of dynamic string literals is not changed.)

This way has following benefits:

* No ugly f-suffix.
* No syntax error on older Ruby.
* We need only a line for each file.

We can write version independent library using frozen static string literals as follows.

* Use the directive at top of the file: # freeze_string: true
  Older Ruby ignore this as a comment.
* Use "...".dup for strings to be modified.
  Older Ruby has small disadvantage: useless dup is called.

Note that the directive effects all static string literals regardless of
single quotes, double quotes, %q-string, %qq-string and here documents.
The reason that the directive is effective not only single quotes is
we want to use escape sequences such as \n in frozen string literals.

Also note that similar directive is already exist:

  % ruby -w -e '
  def m
    end
  '
  -e:3: warning: mismatched indentations at 'end' with 'def' at 2
  % ruby -w -e '# -*- warn_indent: false -*-
  def m
    end
  '

The directive, warn_indent: false, disables "mismatched indentations" warning.

nobu implemented this feature in the meeting.
Please attach the patch, nobu.




-- 
https://bugs.ruby-lang.org/