4
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/Next

Last updated at Posted at 2014-07-09

RuboCop | Style/Next

概要

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

eachなどを利用した際に、next を利用すべき個所に対して警告を出す。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle skip_modifier_ifs if-endを対象とする
EnforcedStyle always if-end, 後置のif ともに対象とする --

対象メソッド

[:collect, :detect, :downto, :each, :find, :find_all,
:inject, :loop, :map!, :map, :reduce, :reverse_each,
:select, :times, :upto]

Next

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

検証プログラム

next.rb

[*1..5].each do |i|
  if i == 3
    puts i
  end
end

[*1..5].each do |i|
  puts i if i == 3
end

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

.rubocop.yml

IfUnlessModifier:
  Enabled: false

Next:
  EnforcedStyle: skip_modifier_ifs
$ rubocop next.rb
Inspecting 1 file
C

Offenses:

next.rb:1:9: C: Use next to skip iteration.
[*1..5].each do |i|
        ^^^^

1 file inspected, 1 offense detected

実行結果 always に設定します

.rubocop.yml

IfUnlessModifier:
  Enabled: false

Next:
  EnforcedStyle: always
$ rubocop next.rb
Inspecting 1 file
C

Offenses:

next.rb:1:9: C: Use next to skip iteration.
[*1..5].each do |i|
        ^^^^
next.rb:7:9: C: Use next to skip iteration.
[*1..5].each do |i|
        ^^^^

1 file inspected, 2 offenses detected

RuboCopまとめ記事

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