LoginSignup
2
2

More than 5 years have passed since last update.

RuboCop | Style/CaseIndentation IndentWhenRelativeTo

Posted at

RuboCop | Style/CaseIndentation IndentWhenRelativeTo

概要

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

when のインデントを case と end どちらに合わせるかを設定します。

設定値一覧

設定対象 設定値 内容 デフォルト
IndentWhenRelativeTo case whenをcaseに合わせてインデントする
IndentWhenRelativeTo end whenをendに合わせてインデントする --

CaseIndentation IndentWhenRelativeTo

各設定値(braces, no_braces)での検証結果をまとめます。

検証プログラム

indent_when_relative_to.rb

i = 1
rets = case i
       when 1
         print '1'
       when 2
         print '2'
       when 3..4
         print '3..4'
       end

print rets

rets = case i
       when 1
         print '1'
       when 2
         print '2'
       when 3..4
         print '3..4'
end

print rets

rets = case i
  when 1
    print '1'
  when 2
    print '2'
  when 3..4
    print '3..4'
end

print rets

rets = case i
when 1
  print '1'
when 2
  print '2'
when 3..4
  print '3..4'
end

print rets

実行結果 case の場合

.rubocop.yml

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

CaseIndentation:
  IndentWhenRelativeTo: 'case'
  IndentOneStep: false
$ rubocop
Inspecting 1 file
C

Offenses:

indent_when_relative_to.rb:25:3: C: Indent when as deep as case.
  when 1
  ^^^^
indent_when_relative_to.rb:27:3: C: Indent when as deep as case.
  when 2
  ^^^^
indent_when_relative_to.rb:29:3: C: Indent when as deep as case.
  when 3..4
  ^^^^
indent_when_relative_to.rb:36:1: C: Indent when as deep as case.
when 1
^^^^
indent_when_relative_to.rb:38:1: C: Indent when as deep as case.
when 2
^^^^
indent_when_relative_to.rb:40:1: C: Indent when as deep as case.
when 3..4
^^^^

1 file inspected, 6 offenses detected

実行結果 end の場合

.rubocop.yml

CaseIndentation:
  IndentWhenRelativeTo: 'end'
  IndentOneStep: false
$ rubocop
Inspecting 1 file
C

Offenses:

indent_when_relative_to.rb:14:8: C: Indent when as deep as end.
       when 1
       ^^^^
indent_when_relative_to.rb:16:8: C: Indent when as deep as end.
       when 2
       ^^^^
indent_when_relative_to.rb:18:8: C: Indent when as deep as end.
       when 3..4
       ^^^^
indent_when_relative_to.rb:25:3: C: Indent when as deep as end.
  when 1
  ^^^^
indent_when_relative_to.rb:27:3: C: Indent when as deep as end.
  when 2
  ^^^^
indent_when_relative_to.rb:29:3: C: Indent when as deep as end.
  when 3..4
  ^^^^

1 file inspected, 6 offenses detected
2
2
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
2
2