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/AlignHash | EnforcedLastArgumentHashStyle

Posted at

RuboCop | Style/AlignHash | EnforcedLastArgumentHashStyle

概要

RuboCopの「Style/AlignHash EnforcedLastArgumentHashStyle」警告について。
メソッドの最後の引数がHashで、複数行で記述する場合の位置スタイルについて。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedLastArgumentHashStyle always_inspect 常に検査する
EnforcedLastArgumentHashStyle always_ignore 常に無視する --
EnforcedLastArgumentHashStyle ignore_implicit 暗黙のHashだけ無視する --
EnforcedLastArgumentHashStyle ignore_explicit 明示のHashだけ無視する --

EnforcedLastArgumentHashStyle

always_inspect, always_ignore, ignore_implicit, ignore_explicit
の各値での rubocop 実行結果をまとめます。

検証プログラム

enforced_last_argument_hash_style.rb

def implicit(options = {})
  'implicit' + options
end

def explicit(options = {})
  'explicit' + options
end

implicit(key1: 1,
  key2: 2)
explicit({ key1: 1,
  key2: 2 })

EnforcedLastArgumentHashStyle always_inspect(デフォルト) でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedLastArgumentHashStyle: 'always_inspect'
BracesAroundHashParameters:
  Enabled: false

暗黙、明示双方のHashが警告対象になります

$ rubocop enforced_last_argument_hash_style.rb
Inspecting 1 file
C

Offenses:

enforced_last_argument_hash_style.rb:10:3: C: Align the elements of a hash literal if they span more than one line.
  key2: 2)
  ^^^^^^^
enforced_last_argument_hash_style.rb:12:3: C: Align the elements of a hash literal if they span more than one line.
  key2: 2 })
  ^^^^^^^

1 file inspected, 2 offenses detected

ちなみに下記のようにすると警告が消えます

def implicit(options = {})
  'implicit'
end

def explicit(options = {})
  'explicit'
end

implicit(key1: 1,
         key2: 2)
explicit({ key1: 1,
           key2: 2 })

EnforcedLastArgumentHashStyle always_ignore でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedLastArgumentHashStyle: 'always_ignore'
BracesAroundHashParameters:
  Enabled: false

暗黙、明示双方のHashが警告対象外になります

$ rubocop enforced_last_argument_hash_style.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

EnforcedLastArgumentHashStyle ignore_implicit でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedLastArgumentHashStyle: 'ignore_implicit'
BracesAroundHashParameters:
  Enabled: false

明示のHashのみ警告対象になります

$ rubocop enforced_last_argument_hash_style.rb
Inspecting 1 file
C

Offenses:

enforced_last_argument_hash_style.rb:12:3: C: Align the elements of a hash literal if they span more than one line.
  key2: 2 })
  ^^^^^^^

1 file inspected, 1 offense detected

EnforcedLastArgumentHashStyle ignore_explicit でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedLastArgumentHashStyle: 'ignore_explicit'
BracesAroundHashParameters:
  Enabled: false

暗黙のHashのみ警告対象になります

$ rubocop enforced_last_argument_hash_style.rb
Inspecting 1 file
C

Offenses:

enforced_last_argument_hash_style.rb:10:3: C: Align the elements of a hash literal if they span more than one line.
  key2: 2)
  ^^^^^^^

1 file inspected, 1 offense detected

補足

この警告は rubocop -a で修正可能です。

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?