-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Style cop idea: prefer map
over each
+ <<
#11878
Comments
Is there a way to detect that multiple loops aren't pushing to a single array? Because I sometimes do this to cut down on array allocations. This cop would force me to use result = []
arr1.each { |x| result << x if something }
arr2.each { |x| result << x if something } |
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby dest = [] src.each { |e| dest << e * 2 } dest src.map { |e| e * 2 } dest = [] src.each { |e| dest << e * 2; puts e } dest ```
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } des # good src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
I've had some hesitations, but I decided to go ahead and create the PR. The following points were particularly complex. I would really appreciate any feedback.
|
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } dest # good src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } dest # good src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } dest # good dest = src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
Fixes rubocop#11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } dest # good dest = src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
Fixes #11878. This PR adds new `Style/MapIntoArray` cop. This cop checks for usages of `each` with `<<`, `push`, or `append` which can be replaced by `map`. It is unsafe, because it can produce false positives if the receiver is not an `Enumerable`. ```ruby # bad dest = [] src.each { |e| dest << e * 2 } dest # good dest = src.map { |e| e * 2 } # good - contains another operation dest = [] src.each { |e| dest << e * 2; puts e } dest ```
I often encounter this pattern, especially amongst junior devs; they start with the
[]
literal, and then build up a list by repeatedly pushing elements into it. It's messy and inefficient.The text was updated successfully, but these errors were encountered: