Rubocop Layout/LineLength: How to ignore lines with comments?

There is a way to ignore cops on a per line basis.

There is also a way to do it via configuration file.

Run rubocop --auto-gen-config and it will generate a file that you can use to disable the offenses.

The command also gives a hint on what to do to load those options.

On a line per line basis, you can enable and disable the cops as well.

# rubocop:disable RuleByName
This is a long line 
# rubocop:enable RuleByName

You can also do more than one rule at a time in your code.

# rubocop:disable BlockComments, AsciiComments

By using an inline directive, the directive becomes valid only for that
line, and it would look like this:

# Thanks to @jnt30 for the comment!
method(argument) # rubocop:disable SomeRule, SomeOtherRule

You can read a ton more about RuboCop in its official manual.

To find all the rule names its worth looking in the rubocop config files

cyberwiz says – “run rubocop -D when I need the rule names rather than looking in the documentation.” Update: This is now the default behavior without the flag.

The -D is now default, so we would get that for “free” now.

Leave a Comment