New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop reports an error on multiline lambdas in scopes #1520

Closed
k-rudy opened this Issue Dec 22, 2014 · 12 comments

Comments

Projects
None yet
8 participants
@k-rudy
Copy link

k-rudy commented Dec 22, 2014

This issue has been originally reported in #1482 but has been closed by mistake as it's still reproducible in v. 0.28.0

Hey, I have a scope defined:

scope :some_scope, ->(param) do
  ...
end

Rubocop claims on Use the 'lambda' method for multi-line lambdas

But when I change it to:

scope :some_scope, lambda do |param|
  ...
end

It claims on Use 'lambda'/'proc' instead of a plain method call.

So what is the correct syntax in this case?

@deivid-rodriguez

This comment has been minimized.

Copy link
Contributor

deivid-rodriguez commented Jan 15, 2015

Hi @k-rudy, I've been there too... 😄

The syntax RuboCop expects you to write is:

scope :some_scope, lambda { |param|
  ...
}

What I don't know is whether there's an option to allow the first syntax you used (which I like better than RuboCop's default).

@k-rudy

This comment has been minimized.

Copy link

k-rudy commented Jan 15, 2015

Hey @deivid-rodriguez, thanks for the hint, it passes. But I think the provided style is a violation of this rule (using {...} for multiline blocks), isn't it?

@deivid-rodriguez

This comment has been minimized.

Copy link
Contributor

deivid-rodriguez commented Jan 15, 2015

Yep, the problem is that the {} and do...end operators have different precedence, so

scope :some_scope, lambda do |param|
  ...
end

actually means

scope(:some_scope, lambda) do |param|
  ...
end

It's just not equivalent...

So there's no way to be consistent here, either enforce single-line-blocks or lambda-multi-line, but not both.

Probably the Style/Lambda cop could be reworked and configurations added to enforce either

  • -> always.
  • lambda always.
  • -> on single line / lambda for multiline (current behaviour).
@k-rudy

This comment has been minimized.

Copy link

k-rudy commented Jan 15, 2015

Ok, now I see it. Thanks @deivid-rodriguez for detailed explanation. It all makes sense now.

@k-rudy k-rudy closed this Jan 15, 2015

@Rajasree

This comment has been minimized.

Copy link

Rajasree commented Feb 24, 2015

Please suggest a syntax, in case if we don't have any param to pass for this multiline scope.

@jonas054

This comment has been minimized.

Copy link
Collaborator

jonas054 commented Mar 13, 2015

@Rajasree Whether the lambda takes a parameter or not, the alternatives are the same. I would suggest adding a local variable:

f = lambda do |param|
  ...
end
scope :some_scope, f

Maybe this looks unidiomatic, and then there's the other alternative, which I thinks is ugly:

scope :some_scope, (lambda do |param|
  ...
end)
@Rajasree

This comment has been minimized.

Copy link

Rajasree commented Mar 16, 2015

@jonas054 Thank you for your response. Its working fine.

@kuon

This comment has been minimized.

Copy link

kuon commented Apr 3, 2016

As I work on swift, js and rust, I find the -> syntax preferable, so:

scope :foo, -> do
end

Whould be prefered.

Do you have plan to allow rubocop to enforce the usage of -> and to allow the above syntax?

@jonas054

This comment has been minimized.

Copy link
Collaborator

jonas054 commented Apr 7, 2016

@kuon Adding a configuration option in Style/Lambda to enforce -> for lambdas regardless of length is definitely doable. Please open a new issue to request that feature.

@bronson

This comment has been minimized.

Copy link
Contributor

bronson commented Oct 14, 2016

Multi-line stabby-procs seem to always work work better than lambdas, and I even think they look better. Hoping for the -> enforcer. Until then, this is a quick fix:

Style/Lambda:
  Enabled: false
@ypresto

This comment has been minimized.

Copy link
Contributor

ypresto commented Dec 28, 2016

FYI: Enforce -> or lambda regardless of line count is already implemented here: #3076

@zinkkrysty

This comment has been minimized.

Copy link

zinkkrysty commented Sep 12, 2017

For people googling and ending up here, this is how you now enforce the -> syntax, regardless of how many lines:

Style/Lambda:
  EnforcedStyle: literal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment