ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

No rule for space after stab

Open saverio-kantox opened this issue 8 years ago • 6 comments

There is no rule for the separation between the stab and its parameters. The parser accepts both with space and without space but it seems that in any other language the 'arrow-like' preferred syntaxes are with space.

# Bad
->(x) { x }
# Good
-> (x) { x }

references: http://eslint.org/docs/rules/arrow-spacing https://google.github.io/styleguide/javaguide.html

saverio-kantox avatar Oct 06 '16 13:10 saverio-kantox

I agree with adding this rule, at least from a consistency standpoint. In many of the projects I've worked in, I've seen both flavors. I don't have a strong preference as to which one should be the default behavior.

joshminnie avatar Oct 06 '16 13:10 joshminnie

Without a space makes more sense to me, the same way you would use parenthesis around the argument of any other method or function, because that's what a lambda is, just without a name.

RomanADavis avatar Oct 06 '16 16:10 RomanADavis

I'd mention the version with space didn't work in Ruby 1.9 when this syntax was introduced, so a lot of code was written without a space and probably we should stick to this for consistency.

bbatsov avatar Dec 12 '16 15:12 bbatsov

@bbatsov we do have an example of incompatible with 1.9 rule. Why not make the same note here as well?

P.S. I personally prefer to write stabby lambas with space, as reads a little better and looks more elegant, however thats rather subjective

TheSmartnik avatar Dec 13 '16 10:12 TheSmartnik

My point is not 1.9 compatibility, but the fact that originally the suggested good syntax didn't work and therefore a lot of code was written without a space. I myself am more used to this syntax for that very reason. I guess someone should check which syntax is more popular and we can just suggest sticking to it.

bbatsov avatar Dec 13 '16 12:12 bbatsov

I understand that -> is a first-class citizen and it's not a method call. However, it hit me by surprise that

Math.log(10).floor # => 2
Math.log (10).floor # => 2.302585092994046

are two different things. Guess why.

Currently, the guide has an example (https://github.com/rubocop-hq/ruby-style-guide/#stabby-lambda-with-args) with no space:

# good
l = ->(x, y) { something(x, y) }

But the guideline is about parentheses around parameters, and the opposing bad example is:

# bad
l = ->x, y { something(x, y) }

I believe we should stick to no space, and clarify that in the same guideline.

pirj avatar Feb 24 '21 16:02 pirj