Skip to content

Commit

Permalink
Merge pull request #7793 from dmolesUC/master
Browse files Browse the repository at this point in the history
Prefer `include?` over `member?` in `Style/CollectionMethods`
  • Loading branch information
koic committed Mar 12, 2020
2 parents 03a58c9 + e9ad8ba commit 24d097b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#7793](https://github.com/rubocop-hq/rubocop/pull/7793): Prefer `include?` over `member?` in `Style/CollectionMethods`. ([@dmolesUC][])
* [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][])

### Bug fixes
Expand Down Expand Up @@ -4389,3 +4390,4 @@
[@djudd]: https://github.com/djudd
[@jemmaissroff]: https://github.com/jemmaissroff
[@nikitasakov]: https://github.com/nikitasakov
[@dmolesUC]: https://github.com/dmolesUC
3 changes: 2 additions & 1 deletion config/default.yml
Expand Up @@ -2455,7 +2455,7 @@ Style/ClassVars:
# Align with the style guide.
Style/CollectionMethods:
Description: 'Preferred collection methods.'
StyleGuide: '#map-find-select-reduce-size'
StyleGuide: '#map-find-select-reduce-include-size'
Enabled: false
VersionAdded: '0.9'
VersionChanged: '0.27'
Expand All @@ -2472,6 +2472,7 @@ Style/CollectionMethods:
inject: 'reduce'
detect: 'find'
find_all: 'select'
member?: 'include?'

Style/ColonMethodCall:
Description: 'Do not use :: for method call.'
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/collection_methods.rb
Expand Up @@ -27,13 +27,15 @@ module Style
# items.inject
# items.detect
# items.find_all
# items.member?
#
# # good
# items.map
# items.map!
# items.reduce
# items.find
# items.select
# items.include?
#
class CollectionMethods < Cop
include MethodPreference
Expand Down
6 changes: 4 additions & 2 deletions manual/cops_style.md
Expand Up @@ -752,24 +752,26 @@ items.collect!
items.inject
items.detect
items.find_all
items.member?

# good
items.map
items.map!
items.reduce
items.find
items.select
items.include?
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
PreferredMethods | `{"collect"=>"map", "collect!"=>"map!", "inject"=>"reduce", "detect"=>"find", "find_all"=>"select"}` |
PreferredMethods | `{"collect"=>"map", "collect!"=>"map!", "inject"=>"reduce", "detect"=>"find", "find_all"=>"select", "member?"=>"include?"}` |

### References

* [https://rubystyle.guide#map-find-select-reduce-size](https://rubystyle.guide#map-find-select-reduce-size)
* [https://rubystyle.guide#map-find-select-reduce-include-size](https://rubystyle.guide#map-find-select-reduce-include-size)

## Style/ColonMethodCall

Expand Down
3 changes: 2 additions & 1 deletion spec/rubocop/cop/style/collection_methods_spec.rb
Expand Up @@ -6,7 +6,8 @@
'collect' => 'map',
'inject' => 'reduce',
'detect' => 'find',
'find_all' => 'select'
'find_all' => 'select',
'member?' => 'include?'
}
}

Expand Down

0 comments on commit 24d097b

Please sign in to comment.