LoginSignup
4
4

More than 5 years have passed since last update.

RuboCop | Style/SpaceBeforeBlockBraces

Posted at

RuboCop | Style/SpaceBeforeBlockBraces

概要

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

ブロック呼び出しに利用するメソッド名とブレースの間に半角スペースを必要とするかどうかをチェックする。
デフォルトでは半角スペースが必要。

SpaceBeforeBlockBraces

設定値一覧

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

検証プログラム

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

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

.rubocop.yml
SpaceBeforeBlockBraces:
  EnforcedStyle: space
$ rubocop space_before_block_braces.rb
Inspecting 1 file
C

Offenses:

space_before_block_braces.rb:2:19: C: Space missing to the left of {.
print [*1..5].each{ |e|puts e }
                  ^

1 file inspected, 1 offense detected

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

.rubocop.yml
SpaceBeforeBlockBraces:
  EnforcedStyle: no_space
  SupportedStyles:
$ rubocop space_before_block_braces.rb
Inspecting 1 file
C

Offenses:

space_before_block_braces.rb:1:19: C: Space detected to the left of {.
print [*1..5].each { |e|puts e }
                  ^

1 file inspected, 1 offense detected

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/space_before_block_braces.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/space_before_block_braces_spec.rb

補足

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

RuboCopまとめ記事

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