5
4

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

Posted at

RuboCop | Style/WhileUntilModifier

概要

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

while, until が1行80文字収まる場合に1行で記述しているかチェックする。
1行あたりの文字数は設定により変更可能。

WhileUntilModifier

検証プログラム

while_until_modifier.rb
def safe_while
  while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  end
end

def out_while
  while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  end
end

def safe_until
  until aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  end
end

def out_until
  until aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  end
end

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

.rubocop.yml
WhileUntilModifier:
  MaxLineLength: 80

LineLength:
  Max: 120
$ rubocop while_until_modifier.rb
Inspecting 1 file
C

Offenses:

while_until_modifier.rb:2:3: C: Favor modifier while usage when having a single-line body.
  while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  ^^^^^
while_until_modifier.rb:14:3: C: Favor modifier until usage when having a single-line body.
  until aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  ^^^^^

1 file inspected, 2 offenses detected

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/while_until_modifier.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/metrics/while_until_modifier_spec.rb

  • 長い文字列のチェック用のヘルパー呼び出しに利用

ソースコードリーディング

様々なテストで利用する文字数チェック用の検証用コードを作成するために、ヘルパーメソッドを利用しています。
テストコードもDRYにする方針のようです。

RuboCopまとめ記事

5
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?