Skip to content
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

Feature: New Performance/MethodMethod #136

Closed
fatkodima opened this issue Jun 8, 2020 · 0 comments · Fixed by #176
Closed

Feature: New Performance/MethodMethod #136

fatkodima opened this issue Jun 8, 2020 · 0 comments · Fixed by #176

Comments

@fatkodima
Copy link
Contributor

fatkodima commented Jun 8, 2020

Is your feature request related to a problem? Please describe.

Related link - https://github.com/JuanitoFatas/fast-ruby#normal-way-to-apply-method-vs-method-code

# bad
array.map(&method(:do_something))

# good
array.map { |x| do_something(x) }

&method(...) construction can be used in many places, e.x. as a callback, passed with other arguments, like here https://github.com/rails/rails/blob/2ad2425fd3b3a58e937f97b1fa05306c4d3166ae/activesupport/lib/active_support/evented_file_update_checker.rb#L103

So we cannot do anything in this case.

But &method construction is mostly used (as I have seen) with Enumerable methods, like https://github.com/rails/rails/blob/2ad2425fd3b3a58e937f97b1fa05306c4d3166ae/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb#L133

I have found a couple of examples in rails, a couple of examples in rubocop, a lot of examples in gitlab codebase.

Describe the solution you'd like

So I propose to detect such cases (with Enumerable methods) and correct them. The simplest correction (without dealing with number of arguments for each specific Enumerable method) is to replace something like this

array.map(&method(:do_something))

with something like this

array.map { |*args| do_something(*args) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants