Help us understand the problem. What is going on with this article?

RuboCop | Style/AlignHash | EnforcedLastArgumentHashStyle

More than 5 years have passed since last update.

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 で修正可能です。

tbpgr
主にRubyを扱う Recruiting Operations 。 tbpgr の読み方は(てぃーびー) 個人ブログでも色々と情報を垂れ流してます。 http://tbpgr.hatenablog.com/
http://tbpgr.hatenablog.com/
studist
「伝えることを、もっと簡単に」をミッションにビジュアルSOPマネジメントプラットフォームのBtoB SaaS「Teachme Biz」を開発・運営するスタートアップ
https://medium.com/studist-dev
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした