Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuboCop: Line is too long ← How to Ignore?

I just added RuboCop to a rails project and installed the Sublime package to see RuboCop suggestions in the editor. I'm trying to figure out how to change the maximum line length from 80 characters, or just ignore the rule completely.

Currently in use:

  • RuboCop (gem)
  • Sublime RuboCop
  • SublimeLinter-rubocop
like image 242
Abram Avatar asked May 14 '16 15:05

Abram


People also ask

How do you ignore RuboCop rules?

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.

How do I ignore RuboCop warning?

The nice thing about disabling Rubocop within code is that you can disable and re-enable cops within a program. So, if you wanted to ignore warnings in a block of code that pertained to 'LineLength' and 'StringLiterals', you could wrap code with comments to disable/enable cops whenever you want.

What is RuboCop in Ruby?

RuboCop is a Ruby code style checking and code formatting tool. It aims to enforce the community-driven Ruby Style Guide.

How do I use allcops/exclude rules in rubocop?

When inspecting a certain directory (or file) given as RuboCop’s command line arguments, patterns listed under AllCops / Exclude are also inspected. If you want to apply AllCops / Exclude rules in this circumstance, add --force-exclusion to the command line argument.

How many empty lines are acceptable in rubocop?

Empty lines do not contribute to the relevant LOC.", and that's why RuboCop has 10 as the default limit. The subject is not entirely without debate. Some studies indicate that long methods are actually less error-prone than short ones, but you have to go with your conviction some times and not blindly follow the numbers.

How many lines of code is too long in Ruby?

The Ruby Style Guide says "Avoid methods longer than 10 LOC (lines of code). Ideally, most methods will be shorter than 5 LOC. Empty lines do not contribute to the relevant LOC.", and that's why RuboCop has 10 as the default limit. The subject is not entirely without debate.

Can rubocop check files that are not included by default?

But, if you’d like RuboCop to check files that are not included by default, you’ll need to pass them in on the command line, or to add entries for them under AllCops / Include. Remember that your configuration files override RuboCops’s defaults.


2 Answers

In your code, you can disable a bunch of lines like this:

# rubocop:disable Layout/LineLength
puts "This line is lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng"
# rubocop:enable Layout/LineLength

Or add this to your .rubocop.yml file to increase the max length:

Layout/LineLength:
  Max: 100
like image 72
Stéphane Bruckert Avatar answered Oct 21 '22 02:10

Stéphane Bruckert


Creating a .rubocop.yml file (keep an eye on the initial . in the filename) in the root of your project, you'll have a bunch of options (check comments for what's your Rubocop version used as the way to handle LineLength has changed):

Metrics/LineLength: # for Rubocop < 0.78.0
Layout/LineLength: # for Rubocop >= 0.78.0
  # This will disable the rule completely, regardless what other options you put
  Enabled: false
  # Change the default 80 chars limit value
  Max: 120
  # If you want the rule only apply to a specific folder/file
  Include:
    - 'app/**/*'
  # If you want the rule not to apply to a specific folder/file
  Exclude:
    - 'db/schema.rb'
like image 72
Alter Lagos Avatar answered Oct 21 '22 03:10

Alter Lagos