2
2

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

Last updated at Posted at 2014-07-07

RuboCop | Style/LambdaCall

概要

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

lambdaの呼び出し方法についてチェックします。
デフォルトでは call を推奨します。

設定値一覧

設定対象 設定値 内容 デフォルト
EnforcedStyle call callメソッドを利用する
EnforcedStyle braces ()を利用する --

LambdaCall

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

検証プログラム

lambda_call.rb

double = ->(x) { x * 2 }

2.step(10, 2).each do |x|
  puts double.call(x)
  puts double.(x)
end

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

.rubocop.yml

LambdaCall:
  EnforcedStyle: call
$ rubocop lambda_call.rb
Inspecting 1 file
C

Offenses:

lambda_call.rb:5:8: C: Prefer the use of lambda.call(...) over lambda.(...).
  puts double.(x)
       ^^^^^^^^^^

1 file inspected, 1 offense detected

実行結果 consistent に設定します

.rubocop.yml

LambdaCall:
  EnforcedStyle: braces
$ rubocop lambda_call.rb
Inspecting 1 file
C

Offenses:

lambda_call.rb:4:8: C: Prefer the use of lambda.(...) over lambda.call(...).
  puts double.call(x)
       ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

補足

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

RuboCopまとめ記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?