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/EachWithObject] Add support for showing warning for positionally misplaced block parameters in reduce
or each_with_object
methods
#8859
Comments
[1,2,3,4].each_with_object([]) do |i, acc|
acc.unshift(i)
nil
end
#=> [4,3,2,1] But this is a good point for |
@dvandersluis Yes correct. With Also it would a really awesome feature to be implemented for |
@yedhink I was just returning nil in my example to prove that I started working yesterday on a cop to catch some cases for |
Is your feature request related to a problem? Please describe.
I'm a beginner in the Ruby world and was just trying out Rubocop. Thanks for creating this wonderful tool.
So when I was coding, I got a suggestion from
rubocop
to replacereduce
witheach_with_object
. I learnt that there exists a method calledeach_with_object
that way, which is super helpful. Thus I auto-formatted the code to followeach_with_object
style and the error was fixed.This is the new rubocop formatted code with
each_with_object
style:-Then later after a few days, inorder to show others in my circle that Rubocop provides such an intelligent suggestion I tried to revert back to the
reduce
based code format, like so:-So after reverting it back to above code, I couldn't get any suggestion to use
each_with_object
overreduce
. I tried all stuffs including explicitly enabling theStyle/EachWithObject:
in.rubocop.yml
. But still no errors were found.Maybe it's my lack of experience, I only later saw the error in above code. When I reverted back to
reduce
based code, I didn't swap positions of theaccumulator
andword
. Inreduce
method, accumulator should come as first block parameter and ineach_with_object
accumulator should come as second block parameter.After making that modification by swapping positions of block params, I got the correct suggestion for
Style/EachWithObject:
for below code:-Describe the solution you'd like
It would be super awesome, if
rubocop
has a rule to check whether theaccumulator
or the first block parameter is the one which is returned fromreduce
method block.Example:- if I give:-
dictionary.reduce(Hash.new(0)) {|acc, word| word}
, then rubocop should atleast give a warning that theThe accumulator "acc" is not the one which is returned.
Similarly if using
each_with_object
, like so:-dictionary.each_with_object(Hash.new(0)) { |acc, word| acc[word]+=1 }
, it should give a warning or error which saysThe accumulator "word" is not the one being modified/accumulated
Describe alternatives you've considered
i'm not sure of the alternatives. I'm new to Ruby.
Additional context
You can paste the above mentioned code-blocks in a ruby file and just run
rubocop
on it to see for yourself, what I mean. If rubocop can show careless mistakes like the one I had done above, then it can save a lot of time for devs I believe.Thanks
The text was updated successfully, but these errors were encountered: