34

I have a class with constants, many constants. And rubocop is complaining about the length of this Class, which I don't care how long it gets.

I want to disable rubocop's error: "Class has too many lines" but the following is not working:

# rubocop:disable ClassLength

Also, the following isn't either:

# rubocop:disable Metrics/ClassLength

What is the correct metric that I need to disable?

fedest
  • 1,190
  • 3
  • 15
  • 35
  • 1
    Possible duplicate of [Rubocop error 'Class definition is too long ruby'](http://stackoverflow.com/questions/20145230/rubocop-error-class-definition-is-too-long-ruby) – j-dexx Oct 24 '16 at 15:15

5 Answers5

44

Try

class Xzy  # rubocop:disable Metrics/ClassLength
AnoE
  • 8,048
  • 1
  • 21
  • 36
  • 1
    This doesn't seem to be working. I add it and then run rubocop but this is the response I get: ```app/constants/user_constants.rb:2:1: C: Class has too many lines. [209/100] class UserConstants ... ^^^^^^^^^^^^^^^^^^^ app/constants/user_constants.rb:214:1: W: [Corrected] Unnecessary disabling of Metrics/ClassLength. # rubocop:disable Metrics/ClassLength ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` – fedest Oct 24 '16 at 16:59
  • 1
    Where did you write the `disable`? Try to add it right on the class definition line. Your error message shows the warning at line 2, but the "unnecessary disabling" at line 214... something is off there. – AnoE Oct 24 '16 at 18:56
  • I was putting it at the end of the file, I put it in the top and it solved the problem. – fedest Oct 24 '16 at 19:59
21

when disabling, be sure to enable again

# rubocop:disable ClassLength
class LongClass
end
# rubocop:enable ClassLength

references:

  1. rubocop/lib/rubocop/cop/metrics/class_length.rb

  2. disabling-cops-within-source-code

SMAG
  • 652
  • 6
  • 12
18

in .rubocop.yml:

Metrics/MethodLength:
  Max: 1000
prograils
  • 2,248
  • 1
  • 28
  • 45
15

Or in .rubocop.yml:

Metrics/ClassLength:
  Exclude:
    - "path/to/your/file.rb"
Dorian
  • 22,759
  • 8
  • 120
  • 116
  • 2
    I prefer this method over editing the Max: value in the .yml file as it is readable and easily changed for snowflake situations that do not warrant a global change. There are pros and cons to this method as compared to the answer that I provided, based on your patterns and practices. – SMAG Dec 04 '18 at 19:31
5

Using the file .rubocop.yml you can add this code to disable it:

Metrics/ClassLength:
  Enabled: false

You can find more information about it in the rubocop configuration page

FonchoRG
  • 81
  • 1
  • 5