3
3

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

Posted at

RuboCop | Style/SingleLineMethods

概要

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

1行で書かれたメソッドがあるかどうかをチェックします。
デフォルトでは空実装の場合のみ1行での記述を許容します。
これは、ダックタイピング用に空メソッドを定義したい場合などに
すっきりとした記述ができるため許容しているものと思われます。

SingleLineMethods

検証プログラム

single_line_methods.rb
def hoge; print :hoge; end

def hige; end

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

.rubocop.yml
SingleLineMethods:
  AllowIfMethodIsEmpty: true
$ rubocop single_line_methods.rb
Inspecting 1 file
C

Offenses:

single_line_methods.rb:1:1: C: Avoid single-line method definitions.
def hoge; print :hoge; end
^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

実行結果 false に設定します

.rubocop.yml
SingleLineMethods:
  AllowIfMethodIsEmpty: false
$rubocop single_line_methods.rb
Inspecting 1 file
C

Offenses:

single_line_methods.rb:1:1: C: Avoid single-line method definitions.
def hoge; print :hoge; end
^^^^^^^^^^^^^^^^^^^^^^^^^^
single_line_methods.rb:3:1: C: Avoid single-line method definitions.
def hige; end
^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

補足

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

RuboCopまとめ記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?