-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
each_with_object style must respect immutable method_args #2321
Comments
Can you provide the message you are receiving? I reproduced the environment you listed and used a test file (shown below) and got a warning which does not relate to
puts [1, 2, 3].reduce(0) { |a, e| a += e } |
If I just replace like it suggested, I got:
Which is right. |
There is the simple condition inside the closure, but in real it could be complex and cannot be modified into a simple one. |
Ok, with that example I am getting the same output; thank you. I toyed around with different syntax and it seems that # this does not generate an offense
r = [1, 2, 3].reduce(0) do |memo, item|
memo + item > 2 ? memo + item : memo
end I was also able to reproduce this error against the latest
I will look into providing a fix in a pull request. |
The point isn't whether the initial value for I'm just opening a PR which fixes this in |
[Fix #2321] Don't replace reduce with each_with_object if accumulator var is assigned to
each_with_object does not work on immutable objects like integer.
http://stackoverflow.com/questions/19064209/how-is-each-with-object-supposed-to-work
In case of
[1, 2, 3].reduce(0) { |a, e| a += e }
rubocop must leave it as is.
rubocop -V
0.34.2 (using Parser 2.2.2.5, running on ruby 2.2.2 x86_64-linux)
Thanks.
The text was updated successfully, but these errors were encountered: