Ruby: What Does the Comment "Frozen_String_Literal: True" Do

Ruby: What does the comment frozen_string_literal: true do?

# frozen_string_literal: true is a magic comment, supported for the first time in Ruby 2.3, that tells Ruby that all string literals in the file are implicitly frozen, as if #freeze had been called on each of them. That is, if a string literal is defined in a file with this comment, and you call a method on that string which modifies it, such as <<, you'll get RuntimeError: can't modify frozen String.

The comment must be on the first line of the file.

In Ruby 2.3, you can use this magic comment to prepare for frozen string literals being the default in Ruby 3.

In Ruby 2.3 run with the --enable=frozen-string-literal flag, and in Ruby 3, string literals are frozen in all files. You can override the global setting with # frozen_string_literal: false.

If you want a string literal to be mutable regardless of the global or per-file setting, you can prefix it with the unary + operator (being careful with operator precedence) or call .dup on it:

# frozen_string_literal: true
"".frozen?
=> true
(+"").frozen?
=> false
"".dup.frozen?
=> false

You can also freeze a mutable (unfrozen) string with unary -.

Source: magic_comment defined in ruby/ruby

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

Nope, it's not enabled by default.

However, you may use Rubocop to append it to the top of your files with Rubocop::Cop::Style::FrozenStringLiteralComment. It's an auto-correctable cop.

According to 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)

Disable frozen string literal comment checking

This one worked for me

Style/FrozenStringLiteralComment:
Enabled: false

rubocop how do you fix Missing magic comment

Just add

# frozen_string_literal: true

to the first line of each Ruby file. Or run

rubocop -a

to allow Rubocop to fix all offenses automatically that it is able to fix.

Btw. I like Rubocop and use it myself, but I wouldn't call the things it finds defects. I see the list more like suggestions or reasons for a discussion with my co-workers.

Within how many lines from the beginning do various directions have to be placed?

(This is a community wiki answer. It's incomplete, so please add your findings.)


#!/usr/bin/env ruby

  • UNIX shebang
  • Must appear at the left margin on the first line of a shell script
  • Interpreted by kernel, some text editors use it to determine the file type

#coding: UTF-8

  • Magic comment for encoding
  • Must appear on the first line or on the second line if the first line is a shebang but can be formatted loosely, e.g. # -*- coding: utf-8 -*-
  • Interpreted by Ruby 1.9+

#frozen_string_literal: true

  • Frozen string literal pragma
  • Appearance?
  • Interpreted by Ruby 2.3

# -*- mode: ruby -*-

  • Emacs file-local variables
  • Must appear on the first line or on the second line if the first line is a shebang
  • Interpreted by Emacs

#vim:set fileencoding=euc-jp

  • Vim modeline
  • Must appear within the first five or last five lines, although this number is configurable with the modelines setting
  • Interpreted by Vim if the modeline setting is enabled (on by default except for root).

Rubocop/Hound recommend freezing string literal class names

It looks like Hound/Rubocop is detecting a violation of the FrozenStringLiteralComment cop.

This cop is designed to help upgrade to Ruby 3.0. It will add the comment # frozen_string_literal: true to the top of files to enable frozen string literals. Frozen string literals will be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.

You can either add the magic comment manually to the top of your files

# frozen_string_literal: true

Or have Rubocop do it for you

$ bundle exec rubocop --auto-correct --only FrozenStringLiteralComment

You can also ignore the cop in your rubocop.yml, Style/FrozenStringLiteralComment



Related Topics



Leave a reply



Submit