-
Notifications
You must be signed in to change notification settings - Fork 21.8k
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
Use index_by and index_with wherever possible #38646
Conversation
Using `index_by` or `index_with` is more concise than `each_with_object` and more performant than `map { ... }.to_h` or `Hash[map { ... }]`.
b69df9c
to
d3599d8
Compare
Dig this! They really are more concise. I’d be curious if Ruby might accept them in core 😄 |
I had to wrap my head to understand what was going on with those method. Even the documentation of |
True, they do take a little bit of learning. I find it quite understandable now though and the benefits far outweigh that initial cost. My mind sees it as index means hash from Enumerable, then by means derive the keys and with means derive the values. Haven’t looked at the index_with documentation since I wrote it, true it really only clicks once you get index_by, and there was contention around the naming of course. Again, I could live with that because it’s better than what was before. |
Upstreaming
Maybe a new proposal would be more successful, but it seems unlikely that the current names would be accepted. I'll think about some alternatives. @rafaelfranca do you find the I've tried to clarify the docs for both methods in #38677. |
I find the concept of |
@eugeneius FYI, RuboCop Rails 2.5.0 including rubocop/rubocop-rails#208 has been released. Thank you for your great code! |
Using
index_by
orindex_with
is more concise thaneach_with_object
and more performant thanmap { ... }.to_h
orHash[map { ... }]
.I found these by writing a RuboCop rule, which I've submitted to the RuboCop Rails project in rubocop/rubocop-rails#208.