class RubbyCop::Cop::Performance::RedundantSortBy

This cop identifies places where `sort_by { … }` can be replaced by `sort`.

@example

@bad
array.sort_by { |x| x }
array.sort_by do |var|
  var
end

@good
array.sort

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/performance/redundant_sort_by.rb, line 32
def autocorrect(node)
  send, = *node
  ->(corrector) { corrector.replace(sort_by_range(send, node), 'sort') }
end
on_block(node) click to toggle source
# File lib/rubbycop/cop/performance/redundant_sort_by.rb, line 25
def on_block(node)
  redundant_sort_by(node) do |send, var_name|
    range = sort_by_range(send, node)
    add_offense(node, range, format(MSG, var_name, var_name))
  end
end

Private Instance Methods

sort_by_range(send, node) click to toggle source
# File lib/rubbycop/cop/performance/redundant_sort_by.rb, line 39
def sort_by_range(send, node)
  range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
end