3
3

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

Posted at

RuboCop | Style/AlignHash | EnforcedHashRocketStyle

概要

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

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedHashRocketStyle key キー左揃
EnforcedHashRocketStyle separator キー右揃、=>を揃える --
EnforcedHashRocketStyle table キー左揃、=>を揃える --

EnforcedHashRocketStyle

key, separator, tableの各値での rubocop 実行結果をまとめます。

検証プログラム

enforced_hash_rocket_style.rb

h_key = {
  'name' => 'tanaka',
   'age' => 33
}

h_separator = {
  'name' => 'tanaka',
   'age' => 33
}

h_table = {
  'name' => 'tanaka',
  'age'  => 33
}

print h_key
print h_separator
print h_table

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

.rubocop.yml

AlignHash:
  EnforcedHashRocketStyle: key

キーが右揃えになっている h_separator のみ警告発生

$rubocop enforced_hash_rocket_style.rb
Inspecting 1 file
C

Offenses:

enforced_hash_rocket_style.rb:8:4: C: Align the elements of a hash literal if they span more than one line.
   'age' => 33
   ^^^^^^^^^^^

1 file inspected, 1 offense detected

EnforcedHashRocketStyle separator でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedHashRocketStyle: separator

キーが左揃えになっている h_key/h_table で警告発生

$rubocop enforced_hash_rocket_style.rb
Inspecting 1 file
C

Offenses:

enforced_hash_rocket_style.rb:3:3: C: Align the elements of a hash literal if they span more than one line.
  'age' => 33
  ^^^^^^^^^^^
enforced_hash_rocket_style.rb:13:3: C: Align the elements of a hash literal if they span more than one line.
  'age'  => 33
  ^^^^^^^^^^^^

1 file inspected, 2 offenses detected

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

.rubocop.yml

AlignHash:
  EnforcedHashRocketStyle: table

ファットアローが揃っていない h_key で警告発生
キーが右揃えになっている h_separator で警告発生

$rubocop enforced_hash_rocket_style.rb
Inspecting 1 file
C

Offenses:

enforced_hash_rocket_style.rb:3:3: C: Align the elements of a hash literal if they span more than one line.
  'age' => 33
  ^^^^^^^^^^^
enforced_hash_rocket_style.rb:8:4: C: Align the elements of a hash literal if they span more than one line.
   'age' => 33
   ^^^^^^^^^^^

1 file inspected, 2 offenses detected

補足

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?