9
8

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

Last updated at Posted at 2014-06-16

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

9
8
2

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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?