ruby-style-guide
ruby-style-guide copied to clipboard
No rule for space after stab
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
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.
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.
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 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
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.
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.