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

RuboCop | Style/AlignHash | EnforcedColonStyle

RuboCop | Style/AlignHash | EnforcedColonStyle

概要

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

設定値一覧

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

EnforcedColonStyle

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

検証プログラム

enforced_colon_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

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

.rubocop.yml

AlignHash:
  EnforcedColonStyle: key

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

$ rubocop enforced_colon_style.rb
Inspecting 1 file
C

Offenses:

enforced_colon_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

EnforcedColonStyle separator でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedColonStyle: separator

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

$ rubocop enforced_colon_style.rb
Inspecting 1 file
C

Offenses:

enforced_colon_style.rb:3:3: C: Align the elements of a hash literal if they span more than one line.
  age: 33
  ^^^^^^^
enforced_colon_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

EnforcedColonStyle table でチェックした場合

.rubocop.yml

AlignHash:
  EnforcedColonStyle: table

Valueが揃っていない h_key で警告発生
キーが右揃えになっている h_separator で警告発生

$ rubocop enforced_colon_style.rb
Inspecting 1 file
C

Offenses:

enforced_colon_style.rb:3:3: C: Align the elements of a hash literal if they span more than one line.
  age: 33
  ^^^^^^^
enforced_colon_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 で修正可能です。

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