5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RuboCop | Style/ClassLength

Posted at

RuboCop | Style/ClassLength

概要

RuboCopの「Style/ClassLength」警告について。

クラスの長さをチェックします。
デフォルトは100行。
また、コメントを数えるかどうか設定可能です。
デフォルトはコメントは数えません。

設定値一覧

設定対象 設定値 内容 デフォルト
CountComments boolean コメントを数えるか false
Max 数値 クラスの文字数 100

ClassLength

各設定値(デフォルト, 任意の値)での検証結果をまとめます。

デフォルト検証

検証プログラム

class_length_default.rb

# Hoge
class Hoge
  def b
    'b'
  end

  def c
    'c'
  end

  def d
    'd'
  end

  def e
    'e'
  end

  def f
    'f'
  end

  def g
    'g'
  end

  def h
    'h'
  end

  def i
    'i'
  end

  def j
    'j'
  end

  def k
    'k'
  end

  def l
    'l'
  end

  def m
    'm'
  end

  def n
    'n'
  end

  def o
    'o'
  end

  def p
    'p'
  end

  def q
    'q'
  end

  def r
    'r'
  end

  def s
    's'
  end

  def t
    't'
  end

  def u
    'u'
  end

  def v
    'v'
  end

  def w
    'w'
  end

  def x
    'x'
  end

  def y
    'y'
  end

  def z
    'z'
  end

  def aa
    'aa'
  end

  def ab
    'ab'
  end

  def ac
    'ac'
  end

  def ad
    'ad'
  end

  def ae
    'ae'
  end

  def af
    'af'
  end

  def ag
    'ag'
  end

  def ah
    'ah'
  end

  def ai
    'ai'
  end
end

実行結果

.rubocop.yml

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

ClassLength:
  CountComments: false  # count full line comments?
  Max: 100
$ rubocop class_length_default.rb
Inspecting 1 file
C

Offenses:

class_length_default.rb:2:1: C: Class definition is too long. [102/100]
class Hoge
^^^^^

1 file inspected, 1 offense detected

任意の値で検証

検証プログラム

class_length_specific.rb

# Hoge
class Hoge
  def b
    # test
    # test
    # test
    # test
    # test
    # test
    # test
    # test
    'b'
  end
end

実行結果 no_braces の場合

.rubocop.yml

ClassLength:
  CountComments: true  # count full line comments?
  Max: 10
$ rubocop class_length_specific.rb
Inspecting 1 file
C

Offenses:

class_length_specific.rb:2:1: C: Class definition is too long. [11/10]
class Hoge
^^^^^

1 file inspected, 1 offense detected
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?