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
Prefer numbered block params over proc conversion #16807
Conversation
…-Za-z._]+)public_method\(:([a-z?_]+)\)\)| { \1\2(_1) }|g'
…-Za-z._]+)method\(:([a-z?_]+)\)\)| { \1\2(_1) }|g'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this looks cleaner and clearer while also being less verbose.
I wondered if this syntax always captured all of the parameters but it looks like it is equivalent after I checked it out in the console. If _1
is used by itself, it seems to capture everything.
irb(main):001:0> [(1..10).to_a].map { _1 }
=> [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
irb(main):002:0> [(1..10).to_a].map { _2 }
=> [2]
irb(main):003:0> [(1..10).to_a].map { _9 }
=> [9]
irb(main):004:0> [(1..10).to_a].map { [_1, _2] }
=> [[1, 2]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I much prefer the prior version. I think it's much more obvious what's going on to non-Rubyists.
Would welcome a RuboCop being created or enabled that enforces the prior style if we're using it inconsistently.
There is a It may be possible to extend that to cover cases where the block param is used as an argument within the block, using the Closing out – sorry for the noise! |
@dduugg This is the bit that I'm curious about (and may encourage me to say you can reopen this): can you elaborate a bit? If this approach is better for type-checking/Sorbet: I'm definitely much more into it than before! |
It's a bit contrived (since it's using instance methods), but here you go We can chew on this one a bit before re-opening though. I have some other, more substantive tickets to open & draft code for… |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You changed my mind, let's do it!
If there's a rubocop
to be able to enforce this way of doing things: that'd be great. If not: writing one might be nice!
I do think that Whether it's |
I'm surprised nothing like that exists in RuboCop yet. Might be nice to upstream it if they're interested. |
brew style
with your changes locally?brew typecheck
with your changes locally?brew tests
with your changes locally?The occasional style of converting a method to a proc for use as a block argument isn't one I've seen elsewhere. I propose replacing it with something more idiomatic (assuming the conversions below are correct, it's honestly a bit confusing to me). This also has the advantage of providing type checking.
(I realize numbered block params have a mixed reception, though I think they're strictly clearer in the cases in this PR. I won't be upset if folks feel otherwise though).