4
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/SpaceInsideHashLiteralBraces

Posted at

RuboCop | Style/SpaceInsideHashLiteralBraces

概要

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

ハッシュリテラルに利用するブレース開始直後、ブレース終了直前、空ブレースの間に半角スペースを必要とするかどうかを
設定・チェックする。
デフォルトでは、ブレース開始直後、ブレース終了直前ともに半角スペースが必要。
空ブレースの場合は半角スペースが不要。

SpaceInsideHashLiteralBraces

設定値一覧

設定対象 対象 内容 デフォルト
EnforcedStyle space 半角スペースが必要
EnforcedStyle no_space 半角スペースが不要 --
EnforcedStyleForEmptyBraces space 空ブレースに半角スペースが必要 --
EnforcedStyleForEmptyBraces no_space 空ブレースに半角スペースが不要

検証プログラム

space_inside_hash_literal_braces.rb
h = { key: :value }
print h
h = {key: :value }
print h
h = { key: :value }
print h
h = {key: :value}
print h
h = {}
print h
h = { }
print h

実行結果 デフォルト の場合

.rubocop.yml
SpaceInsideHashLiteralBraces:
  EnforcedStyle: space
  EnforcedStyleForEmptyBraces: no_space
$ rubocop space_inside_hash_literal_braces.rb
Inspecting 1 file
C

Offenses:

space_inside_hash_literal_braces.rb:3:5: C: Space inside { missing.
h = {key: :value }
    ^
space_inside_hash_literal_braces.rb:7:5: C: Space inside { missing.
h = {key: :value}
    ^
space_inside_hash_literal_braces.rb:7:17: C: Space inside } missing.
h = {key: :value}
                ^
space_inside_hash_literal_braces.rb:11:6: C: Space inside empty hash literal braces detected.
h = { }
     ^

1 file inspected, 4 offenses detected

実行結果 EnforcedStyle を no_space, EnforcedStyleForEmptyBraces を space に設定した場合

.rubocop.yml
SpaceInsideHashLiteralBraces:
  EnforcedStyle: no_space
  EnforcedStyleForEmptyBraces: space
$ rubocop space_inside_hash_literal_braces.rb
Inspecting 1 file
C

Offenses:

space_inside_hash_literal_braces.rb:1:6: C: Space inside { detected.
h = { key: :value }
     ^
space_inside_hash_literal_braces.rb:1:18: C: Space inside } detected.
h = { key: :value }
                 ^
space_inside_hash_literal_braces.rb:3:17: C: Space inside } detected.
h = {key: :value }
                ^
space_inside_hash_literal_braces.rb:5:6: C: Space inside { detected.
h = { key: :value }
     ^
space_inside_hash_literal_braces.rb:5:18: C: Space inside } detected.
h = { key: :value }
                 ^
space_inside_hash_literal_braces.rb:9:5: C: Space inside empty hash literal braces missing.
h = {}
    ^

1 file inspected, 6 offenses detected

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb

補足

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

RuboCopまとめ記事

4
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
4
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?