LoginSignup
20
14

More than 5 years have passed since last update.

RuboCop | Style/ClassAndModuleChildren EnforcedStyle

Last updated at Posted at 2014-06-23

RuboCop | Style/ClassAndModuleChildren EnforcedStyle

概要

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

クラス内のクラスなどを定義する際の形式を強制します。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle nested ネスト形式
EnforcedStyle compact ParentClass::ChildClass形式でコンパクトに記述する --

ClassAndModuleChildren EnforcedStyle

各設定値(nested, compact)での検証結果をまとめます。

検証プログラム

enforced_style.rb

# Hoge
class Hoge
  # Hoge::ChildHoge
  class ChildHoge
  end
end

# Hoge::ChildHoge
class Hoge::ChildHoge
end

実行結果 nested の場合

.rubocop.yml

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

ClassAndModuleChildren:
  EnforcedStyle: nested

compact形式の場合に警告が発生します。

$ rubocop enforced_style.rb
Inspecting 1 file
C

Offenses:

enforced_style.rb:9:7: C: Use nested module/class definitions instead of compact style.
class Hoge::ChildHoge
      ^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

実行結果 compact の場合

.rubocop.yml

ClassAndModuleChildren:
  EnforcedStyle: compact

nested形式の場合に警告が発生します。

$ rubocop enforced_style.rb
Inspecting 1 file
C

Offenses:

enforced_style.rb:2:7: C: Use compact module/class definition instead of nested style.
class Hoge
      ^^^^

1 file inspected, 1 offense detected
20
14
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
20
14