class RuboCop::Cop::Style::RedundantSortBy
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_BLOCK
- MSG_NUMBLOCK
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 25 def on_block(node) redundant_sort_by_block(node) do |send, var_name| range = sort_by_range(send, node) add_offense(range, message: format(MSG_BLOCK, var: var_name)) do |corrector| corrector.replace(range, 'sort') end end end
on_numblock(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 35 def on_numblock(node) redundant_sort_by_numblock(node) do |send| range = sort_by_range(send, node) add_offense(range, message: format(MSG_NUMBLOCK)) do |corrector| corrector.replace(range, 'sort') end end end
Private Instance Methods
sort_by_range(send, node)
click to toggle source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 57 def sort_by_range(send, node) range_between(send.loc.selector.begin_pos, node.loc.end.end_pos) end