13

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
  • 204,283
  • 31
  • 260
  • 272
american-ninja-warrior
  • 7,397
  • 11
  • 46
  • 80

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
  • 100,941
  • 9
  • 101
  • 131
  • Instead of all files, ` rubocop -a` is only `Inspecting 2 files` and only changed the `Rakefile`. How do I make rubocop scan the whole project? I already tried things with `*` and `**` without success – Cadoiz Mar 27 '23 at 06:14
  • @Cadoiz `rubocop -a` will fix all safely auto-correctable offenses in your project, depending on your Rubocop's configuration. If it does not, then you might want to consider opening a new question with more details about your project, your configuration and the types of offenses that command does not fix. – spickermann Mar 27 '23 at 06:30
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, now accepted answer):

rubocop --only Style/FrozenStringLiteralComment -A

You can view more information about autocorrect here.

Cadoiz
  • 1,446
  • 21
  • 31
jdgray
  • 1,863
  • 1
  • 11
  • 19
  • 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
  • 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
  • I disagree with @FallenAngel as upper-case `-A` gives me `invalid option: -A For usage information, use --help`. I'm using `ruby 2.6.6p146` – Cadoiz Mar 27 '23 at 06:23
4

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

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:

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

Cadoiz
  • 1,446
  • 21
  • 31
anothermh
  • 9,815
  • 3
  • 33
  • 52