class RuboCop::Cop::Style::CollectionMethods
items.select
items.find
items.reduce
items.map!
items.map
# good
items.find_all
items.detect
items.inject
items.collect!
items.collect
# bad
@example
The default mapping for ‘PreferredMethods` behaves as follows.
find: detect
PreferredMethods:
Style/CollectionMethods:
e.g. to use `detect` over `find`:
You can customize the mapping from undesired method to desired method.
can yield some false positives.
Enumerable or not (static analysis limitation), so this cop
Unfortunately we cannot actually know if a method is from
from the Enumerable module.
This cop enforces the use of consistent method names
def autocorrect(node)
def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source)) end end
def check_method_node(node)
def check_method_node(node) return unless preferred_methods[node.method_name] add_offense(node, location: :selector) end
def message(node)
def message(node) format(MSG, prefer: preferred_method(node.method_name), current: node.method_name) end
def on_block(node)
def on_block(node) check_method_node(node.send_node) end
def on_send(node)
def on_send(node) return unless node.arguments.one? && node.first_argument.block_pass_type? check_method_node(node) end