3
5

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/SpaceAroundEqualsInParameterDefault

Posted at

RuboCop | Style/SpaceAroundEqualsInParameterDefault

概要

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

オプション引数のデフォルト値の設定時に半角スペースを必要とするかどうかを
設定・チェックする。

設定値一覧

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

SpaceAroundEqualsInParameterDefault

検証プログラム

space_around_equals_in_parameter_default.rb
def hoge(a, b=nil)
  print a + b
end

def hoge(a, b =nil)
  print a + b
end

def hoge(a, b= nil)
  print a + b
end

def hoge(a, b = nil)
  print a + b
end

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

.rubocop.yml
SpaceAroundEqualsInParameterDefault:
  EnforcedStyle: space
$ rubocop space_around_equals_in_parameter_default.rb
Inspecting 1 file
C

Offenses:

space_around_equals_in_parameter_default.rb:1:14: C: Surrounding space missing in default value assignment.
def hoge(a, b=nil)
             ^
space_around_equals_in_parameter_default.rb:5:14: C: Surrounding space missing in default value assignment.
def hoge(a, b =nil)
             ^^
space_around_equals_in_parameter_default.rb:9:14: C: Surrounding space missing in default value assignment.
def hoge(a, b= nil)
             ^^

1 file inspected, 3 offenses detected

実行結果 no_space に設定します

.rubocop.yml
SpaceAroundEqualsInParameterDefault:
  EnforcedStyle: no_space
$rubocop space_around_equals_in_parameter_default.rb
Inspecting 1 file
C

Offenses:

space_around_equals_in_parameter_default.rb:5:14: C: Surrounding space detected in default value assignment.
def hoge(a, b =nil)
             ^^
space_around_equals_in_parameter_default.rb:9:14: C: Surrounding space detected in default value assignment.
def hoge(a, b= nil)
             ^^
space_around_equals_in_parameter_default.rb:13:14: C: Surrounding space detected in default value assignment.
def hoge(a, b = nil)
             ^^^

1 file inspected, 3 offenses detected

補足

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

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb

RuboCopまとめ記事

3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?