LoginSignup
5
5

More than 5 years have passed since last update.

RuboCop | Style/ParameterLists

Last updated at Posted at 2014-07-15

RuboCop | Style/ParameterLists

概要

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

引数の数をチェックします。
デフォルトは5個より多いと警告を出します。
デフォルトではキーワード引数もカウント対象としますが、
対象外に設定することも可能です。

設定値一覧

設定対象 設定値 内容 デフォルト
CountKeywordArgs true キーワード引数を対象にする
CountKeywordArgs false キーワード引数を対象にする --

ParameterLists

各設定値での検証結果をまとめます。

検証プログラム

parameter_lists.rb
def hoge(a, b, c, d, e)
  print a, b, c, d, e
end

def hige(a, b, c, d, e, f)
  print a, b, c, d, e, f
end

def hage(a, b, c, d, e, optional: {})
  print a, b, c, d, e, optional
end

実行結果 デフォルト の場合

.rubocop.yml
ParameterLists:
  Max: 5
  CountKeywordArgs: true
$ rubocop parameter_lists.rb
Inspecting 1 file
C

Offenses:

parameter_lists.rb:5:9: C: Avoid parameter lists longer than 5 parameters.
def hige(a, b, c, d, e, f)
        ^^^^^^^^^^^^^^^^^^
parameter_lists.rb:9:9: C: Avoid parameter lists longer than 5 parameters.
def hage(a, b, c, d, e, optional: {})
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

実行結果 Max を 6 に設定します

.rubocop.yml
ParameterLists:
  Max: 6
  CountKeywordArgs: true
$ rubocop parameter_lists.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

実行結果 Max を 5, CountKeywordArgsfalse に設定します

.rubocop.yml

ParameterLists:
  Max: 5
  CountKeywordArgs: false
$ rubocop parameter_lists.rb
Inspecting 1 file
C

Offenses:

parameter_lists.rb:5:9: C: Avoid parameter lists longer than 5 parameters.
def hige(a, b, c, d, e, f)
        ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

RuboCopまとめ記事

5
5
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
5