LoginSignup
16
11

More than 5 years have passed since last update.

RuboCop | Style/BracesAroundHashParameters EnforcedStyle

Posted at

RuboCop | Style/BracesAroundHashParameters EnforcedStyle

概要

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

メソッドの最後の引数にハッシュを利用した際のブレースの有無をチェックします。
デフォルトではブレースありを有効とします。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle braces ブレースあり
EnforcedStyle no_braces ブレースなし --

BracesAroundHashParameters EnforcedStyle

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

検証プログラム

enforced_style.rb

def some_method(options = {})
  print options
end

some_method({ key1: :value1, key2: :value2 })
some_method(key1: :value1, key2: :value2)

実行結果 braces の場合

.rubocop.yml

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

BracesAroundHashParameters:
  EnforcedStyle: braces
$ rubocop enforced_style.rb
Inspecting 1 file
C

Offenses:

enforced_style.rb:6:13: C: Missing curly braces around a hash parameter.
some_method(key1: :value1, key2: :value2)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

実行結果 no_braces の場合

.rubocop.yml

BracesAroundHashParameters:
  EnforcedStyle: no_braces
$ rubocop enforced_style.rb
Inspecting 1 file
C

Offenses:

enforced_style.rb:5:13: C: Redundant curly braces around a hash parameter.
some_method({ key1: :value1, key2: :value2 })
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

補足

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

16
11
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
16
11