rails 应用程序中确实需要“# frozen_string_literal: true”???我需要把它放在每个文件中吗?

'# frozen_string_literal: true' is really needed in a rails application??? do i need to put it in each file?

我正在编辑的 rails 应用程序的每个文件都显示此警告,missing frozen string literal comment,我知道这用于保留内存并避免为所有字符串文字重新分配内存,但是有必要在 rails 上使用还是默认启用?如果可以,如何全局启用它?

不,默认情况下未启用。

但是,您可以使用 Rubocop 通过 Rubocop::Cop::Style::FrozenStringLiteralComment 将其附加到文件的顶部。这是一个可自动纠正的警察。

根据Holger Just

You can actually enable it globally by invoking the ruby interpreter with ruby --enable=frozen-string-literal. However, this is usually a bad idea and will break in various subtle ways unless you are very sure that all files in all your gems and dependencies actually expect frozen literals (which is generally not the case)