LoginSignup
5
5

More than 5 years have passed since last update.

RuboCop | Style/BlockNesting

Posted at

RuboCop | Style/BlockNesting

概要

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

この警告は、if文等のネストをチェックしてくれます。
デフォルトは 3 階層に設定されています。

名前はBlockNestingになっていますが、Rubyのブロックはこの警告のチェック対象外。

ネストのチェック対象

  • :case
  • :if
  • :while
  • :until
  • :for

BlockNesting

デフォルトの設定値での検証結果をまとめます。

検証プログラム

block_nesting.rb

if (msg == 1)
  if (msg == 2)
    if (msg == 3)
      if (msg == 4)
        print 'if'
      else
        print 'else4'
      end
    else
      print 'else3'
    end
  else
    print 'else2'
  end
else
  print 'else1'
end

実行結果

$ rubocop block_nesting.rb
Inspecting 1 file
C

Offenses:

block_nesting.rb:4:7: C: Avoid more than 3 levels of block nesting.
      if (msg == 4)
      ^^^^^^^^^^^^^

1 file inspected, 1 offense detected
5
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
5
5