Class: Rubocop::Cop::CollectionMethods

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

Constant Summary collapse

PREFERRED_METHODS =
{
  'collect' => 'map',
  'inject' => 'reduce',
  'detect' => 'find',
  'find_all' => 'select',
}

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, #has_report?, inherited, #initialize

Constructor Details

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

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



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

def inspect(file, source, tokens, sexp)
  each(:call, sexp) do |s|
    s.drop(2).each_slice(2) do |m|
      method_name = m[1][1]
      if PREFERRED_METHODS[method_name]
        add_offence(
          :convention,
          m[1][2].lineno,
          "Prefer #{PREFERRED_METHODS[method_name]} over #{method_name}."
        )
      end
    end
  end
end