Class: RuboCop::Cop::Performance::SortReverse

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
SortBlock
Defined in:
lib/rubocop/cop/performance/sort_reverse.rb

Overview

Identifies places where ‘sort { |a, b| b <=> a }` can be replaced by a faster `sort.reverse`.

Examples:

# bad
array.sort { |a, b| b <=> a }

# good
array.sort.reverse

Constant Summary collapse

MSG =
'Use `%<prefer>s` instead.'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rubocop/cop/performance/sort_reverse.rb', line 22

def on_block(node)
  sort_with_block?(node) do |send, var_a, var_b, body|
    replaceable_body?(body, var_b, var_a) do
      register_offense(send, node)
    end
  end
end

#on_numblock(node) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/performance/sort_reverse.rb', line 30

def on_numblock(node)
  sort_with_numblock?(node) do |send, arg_count, body|
    next unless arg_count == 2

    replaceable_body?(body, :_2, :_1) do
      register_offense(send, node)
    end
  end
end