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

RuboCop | Style/AlignHash | EnforcedHashRocketStyle

More than 5 years have passed since last update.

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

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
ユーザーは見つかりませんでした