Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubocop Linelength: How to ignore lines with comments?

Tags:

ruby

rubocop

People also ask

How do you ignore a rubocop rule?

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.

What is rubocop todo?

RuboCop's TODO file is generated by the command line option auto-gen-config : rubocop --auto-gen-config. This action runs rubocop across your entire code base, then constructs a set of rule exceptions that effectively 'ignore' code that doesn't fit into the existing guidelines.


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.


It's possible to define regex patterns to automatically ignore certain lines in rubocop.yml, so you could choose to ignore all lines starting with a # character:

Layout/LineLength:
  Max: 80
  IgnoredPatterns: ['\A#']

This could be improved so that "indented" comment lines (i.e. whitespace followed by a # character) are also ignored, if that's what you want.

Note that this doesn't account for lines of code that end with a comment, though:

some_code(that_does_something) # This line would NOT be ignored by Rubocop.

You can use the following comment with rubocop to ignore a specific rule:

# rubocop:disable Layout/LineLength
def this_could_be_a_very_long_line_that_extends_forever_into_infinity
end
# rubocop:enable Layout/LineLength

You can also ignore whole files by adding them to .rubocop.yml:

AllCops:
  Exclude:
    - path/to/file.rb

i think the basic idea here is that you want to enforce line length, no matter what is after n characters. the default to 80 characters is some cargo cult for old terminal windows that could only hold that number of chars. the only option that i saw in the code is an option to allow urls that might exceed the character limit.

you can ignore whole files, i guess that's not what you are looking for.