13
11

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

Last updated at Posted at 2014-07-12

RuboCop | Style/MethodLength

概要

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

メソッドの行数をチェックします。
デフォルトは10行で、コメントは対象外とします。

設定値一覧

設定対象 設定値 内容 デフォルト
CountComments false コメントを警告対象外にする
CountComments true コメントを警告対象にする --
Max 10 警告行数

MethodLength

各設定値での検証結果をまとめます。

検証プログラム

non_nil_check.rb

def hoge1
  # commnet
  puts '1'
  puts '2'
  puts '3'
  puts '4'
  puts '5'
  puts '6'
  puts '7'
  puts '8'
  puts '9'
  puts '10'
end

def hoge2
  puts '1'
  puts '2'
  puts '3'
  puts '4'
  puts '5'
  puts '6'
  puts '7'
  puts '8'
  puts '9'
  puts '10'
end

def hoge3
  puts '1'
  puts '2'
  puts '3'
  puts '4'
  puts '5'
  puts '6'
  puts '7'
  puts '8'
  puts '9'
  puts '10'
  puts '11'
end

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

.rubocop.yml

MethodLength:
  CountComments: false  # count full line comments?
  Max: 10
$ rubocop method_length.rb
Inspecting 1 file
C

Offenses:

method_length.rb:28:1: C: Method has too many lines. [11/10]
def hoge3
^^^

1 file inspected, 1 offense detected

実行結果 CountComments を true に設定します

.rubocop.yml

MethodLength:
  CountComments: true  # count full line comments?
  Max: 10
$ rubocop method_length.rb
Inspecting 1 file
C

Offenses:

method_length.rb:1:1: C: Method has too many lines. [11/10]
def hoge1
^^^
method_length.rb:28:1: C: Method has too many lines. [11/10]
def hoge3
^^^

1 file inspected, 2 offenses detected

実行結果 CountComments を true, Max を 11 に設定します

.rubocop.yml

MethodLength:
  CountComments: true  # count full line comments?
  Max: 11
$ rubocop method_length.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

RuboCopまとめ記事

13
11
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
13
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?