LoginSignup
4
4

More than 5 years have passed since last update.

RuboCop | Style/SpaceInsideBlockBraces

Posted at

RuboCop | Style/SpaceInsideBlockBraces

概要

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

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

SpaceInsideBlockBraces

設定値一覧

設定対象 対象 内容 デフォルト
EnforcedStyle space 半角スペースが必要
EnforcedStyle no_space 半角スペースが不要 --
EnforcedStyleForEmptyBraces space 空ブレースに半角スペースが必要 --
EnforcedStyleForEmptyBraces no_space 空ブレースに半角スペースが不要
SpaceBeforeBlockParameters true ブロック引数の前に半角スペースが必要
SpaceBeforeBlockParameters false ブロック引数の前に半角スペースが不要 --

検証プログラム

space_inside_block_braces.rb
print [*1..5].each { |e|puts e }
print [*1..5].each {|e|puts e }
print [*1..5].each { |e|puts e}
print [*1..5].each {}
print [*1..5].each { }

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

.rubocop.yml
Style/SpaceInsideBlockBraces:
  EnforcedStyle: space
  EnforcedStyleForEmptyBraces: no_space
  SpaceBeforeBlockParameters: true
$ rubocop space_inside_block_braces.rb
Inspecting 1 file
C

Offenses:

space_inside_block_braces.rb:2:20: C: Space between { and | missing.
print [*1..5].each {|e|puts e }
                   ^^
space_inside_block_braces.rb:3:31: C: Space missing inside }.
print [*1..5].each { |e|puts e}
                              ^
space_inside_block_braces.rb:5:21: C: Space inside empty braces detected.
print [*1..5].each { }
                    ^

1 file inspected, 3 offenses detected

実行結果 no_space に設定した場合

.rubocop.yml
Style/SpaceInsideBlockBraces:
  EnforcedStyle: no_space
  EnforcedStyleForEmptyBraces: space
  SpaceBeforeBlockParameters: false
$ rubocop space_inside_block_braces.rb
Inspecting 1 file
C

Offenses:

space_inside_block_braces.rb:1:21: C: Space between { and | detected.
print [*1..5].each { |e|puts e }
                    ^
space_inside_block_braces.rb:1:31: C: Space inside } detected.
print [*1..5].each { |e|puts e }
                              ^
space_inside_block_braces.rb:2:30: C: Space inside } detected.
print [*1..5].each {|e|puts e }
                             ^
space_inside_block_braces.rb:3:21: C: Space between { and | detected.
print [*1..5].each { |e|puts e}
                    ^
space_inside_block_braces.rb:4:20: C: Space missing inside empty braces.
print [*1..5].each {}
                   ^^

1 file inspected, 5 offenses detected

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/space_inside_block_braces.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/space_inside_block_braces_spec.rb

補足

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

RuboCopまとめ記事

4
4
1

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
4