LoginSignup
5
5

More than 5 years have passed since last update.

RuboCop | Style/For

Last updated at Posted at 2014-07-01

RuboCop | Style/For

概要

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

ForとEachのどちらを使っているかをチェックします。
デフォルトではForに対して警告を出します。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle for forを警告対象にする --
EnforcedStyle each eachを警告対象にする

For

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

検証プログラム

for.rb

for num1 in 1..5 do
  puts num1
end

(1..5).each do |num2|
  puts num2
end

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

.rubocop.yml

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

For:
  EnforcedStyle: each
$ rubocop for.rb
Inspecting 1 file
C

Offenses:

for.rb:1:1: C: Prefer each over for.
for num1 in 1..5 do
^^^

1 file inspected, 1 offense detected

実行結果 for の場合

.rubocop.yml

For:
  EnforcedStyle: for
$ rubocop for.rb
Inspecting 1 file
C

Offenses:

for.rb:5:8: C: Prefer for over each.
(1..5).each do |num2|
       ^^^^

1 file inspected, 1 offense detected
5
5
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
5