7
6

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

Last updated at Posted at 2014-07-18

RuboCop | Style/PredicateName

概要

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

has_, is_, have_ など,特定の文字列で始まるメソッド名をチェックします。

設定値一覧

設定対象 対象 内容 デフォルト
NamePrefixBlacklist is_ is_で始まっていたら警告
NamePrefixBlacklist has_ has_で始まっていたら警告
NamePrefixBlacklist have_ have_で始まっていたら警告

PredicateName

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

検証プログラム

predicate_name.rb
def is_head?
  true
end

def has_head?
  true
end

def have_head?
  true
end

def hoge_head?
  true
end

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

.rubocop.yml
PredicateName:
  NamePrefixBlacklist:
    - is_
    - has_
    - have_
$ rubocop predicate_name.rb
Inspecting 1 file
C

Offenses:

predicate_name.rb:1:5: C: Rename is_head? to head?.
def is_head?
    ^^^^^^^^
predicate_name.rb:5:5: C: Rename has_head? to head?.
def has_head?
    ^^^^^^^^^
predicate_name.rb:9:5: C: Rename have_head? to head?.
def have_head?
    ^^^^^^^^^^

1 file inspected, 3 offenses detected

実行結果 独自定義で hoge を追加, is/has_/have_ は無効にします

.rubocop.yml
PredicateName:
  NamePrefixBlacklist:
    - hoge_
$ rubocop predicate_name.rb
Inspecting 1 file
C

Offenses:

predicate_name.rb:13:5: C: Rename hoge_head? to head?.
def hoge_head?
    ^^^^^^^^^^

1 file inspected, 1 offense detected

RuboCopまとめ記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?