class RuboCop::Cop::Style::CollectionMethods
items.include?
items.select
items.find
items.reduce
items.flat_map
items.map!
items.map
# good
items.member?
items.find_all
items.detect
items.inject
items.collect_concat
items.collect!
items.collect
# bad
# These examples are based on the default mapping for ‘PreferredMethods`.
@example
this cop may register false positives.
being able to determine if the receiver is an Enumerable or not, so
This cop is unsafe because it finds methods by name, without actually
@safety
find: detect
PreferredMethods:
Style/CollectionMethods:
e.g. to use `detect` over `find`:
You can customize the mapping from undesired method to desired method.
from the Enumerable module.
Enforces the use of consistent method names
def check_method_node(node)
def check_method_node(node) return unless preferred_methods[node.method_name] message = message(node) add_offense(node.loc.selector, message: message) do |corrector| corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source)) end end
def implicit_block?(node)
def implicit_block?(node) return false unless node.arguments.any? node.last_argument.block_pass_type? || (node.last_argument.sym_type? && methods_accepting_symbol.include?(node.method_name.to_s)) end
def message(node)
def message(node) format(MSG, prefer: preferred_method(node.method_name), current: node.method_name) end
def methods_accepting_symbol
Some enumerable methods accept a bare symbol (ie. _not_ Symbol#to_proc) instead
def methods_accepting_symbol Array(cop_config['MethodsAcceptingSymbol']) 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 implicit_block?(node) check_method_node(node) end