123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Performance::RedundantSortBlock

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AutoCorrector, Base
Instance Chain:
self, ::RuboCop::Cop::SortBlock, RangeHelp, Base
Inherits: Base
  • Object
Defined in: lib/rubocop/cop/performance/redundant_sort_block.rb

Overview

Identifies places where sort { |a, b| a <⇒ b } can be replaced with sort.

Examples:

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

# good
array.sort

Constant Summary

Instance Method Summary

Instance Method Details

#on_block(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_sort_block.rb', line 21

def on_block(node)
  return unless (send, var_a, var_b, body = sort_with_block?(node))

  replaceable_body?(body, var_a, var_b) do
    register_offense(send, node)
  end
end

#on_numblock(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_sort_block.rb', line 29

def on_numblock(node)
  return unless (send, arg_count, body = sort_with_numblock?(node))
  return unless arg_count == 2

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

#register_offense(send, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_sort_block.rb', line 40

def register_offense(send, node)
  range = sort_range(send, node)

  add_offense(range) do |corrector|
    corrector.replace(range, 'sort')
  end
end