12

I have a library of ruby code, and to look for defects I run

$ rubocop

And I get

$ rubocop
Inspecting 153 files
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCWWCCCCC

Offenses:

Gemfile:1:1: C: Missing magic comment # frozen_string_literal: true.
source "https://rubygems.org"

What modifications are required in my Gemfile to make rubocop not complain?

j08691
  • 197,815
  • 30
  • 248
  • 265
american-ninja-warrior
  • 6,237
  • 8
  • 39
  • 69

4 Answers4

28

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.

spickermann
  • 89,540
  • 8
  • 92
  • 120
12

If you want to be more specific and run rubocop for only # frozen_string_literal: true you can use the --only flag option:

Run only the specified cop(s) and/or cops in the specified departments.

To view those files:

rubocop --only Style/FrozenStringLiteralComment

To autocorrect those specific files use the -A flag (as mentioned in a previous answer):

rubocop --only Style/FrozenStringLiteralComment -A

You can view more information about autocorrect here.

Fa11enAngel
  • 4,455
  • 2
  • 36
  • 34
jdgray
  • 1,793
  • 1
  • 10
  • 18
  • Also, to correct the "Add an empty line after magic comments." offenses that running this command will likely produce you can use this: rubocop --only "Style/FrozenStringLiteralComment, Layout/EmptyLineAfterMagicComment" -A – Meursault Aug 29 '20 at 17:28
  • Hey good call out! Just noticed this in the docs: https://docs.rubocop.org/rubocop/0.89/usage/auto_correct.html – jdgray Aug 31 '20 at 15:05
  • The letter '-A' must be upper case, as frozen_string_literal is not always safe - or rubocop can not verify wether it is really safe for the file. '-a' is only for safe corrections, see docs linked in answer. – Fa11enAngel Dec 13 '21 at 09:19
2

Try running Rubocop with the -D option:

rubocop -D
Inspecting 1 file
C

Offenses:

spec/rails_helper.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
require 'spec_helper'
^

Adding -D will cause Rubocop to print the name of the cop that was violated, in this case Style/FrozenStringLiteralComment. You can then search for that cop in the Rubocop documentation:

http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FrozenStringLiteralComment

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+.

anothermh
  • 7,000
  • 3
  • 28
  • 45
2

If you want to ignore it, add to your .rubocop.yml

Style/FrozenStringLiteralComment:
  Enabled: false

But may be you want to know what "Magic comment" is, especially if you're using Ruby 2.x