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_ITBLOCK
- MSG_NUMBLOCK
Public Instance Methods
Source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 26 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
Source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 46 def on_itblock(node) redundant_sort_by_itblock(node) do |send| range = sort_by_range(send, node) add_offense(range, message: MSG_ITBLOCK) do |corrector| corrector.replace(range, 'sort') end end end
Source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 36 def on_numblock(node) redundant_sort_by_numblock(node) do |send| range = sort_by_range(send, node) add_offense(range, message: MSG_NUMBLOCK) do |corrector| corrector.replace(range, 'sort') end end end
Private Instance Methods
Source
# File lib/rubocop/cop/style/redundant_sort_by.rb, line 73 def sort_by_range(send, node) range_between(send.loc.selector.begin_pos, node.loc.end.end_pos) end