Class: Rubocop::Cop::CollectionMethods

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/collection_methods.rb

Constant Summary collapse

MSG =
'Prefer %s over %s.'
PREFERRED_METHODS =
{
  collect: 'map',
  inject: 'reduce',
  detect: 'find',
  find_all: 'select'
}

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/collection_methods.rb', line 15

def on_send(node)
  receiver, method_name, *_args = *node

  # a simple(but flawed way) to reduce false positives
  if receiver && PREFERRED_METHODS[method_name]
    add_offence(
      :convention,
      node.loc.line,
      sprintf(MSG, PREFERRED_METHODS[method_name], method_name)
    )
  end
end