class RuboCop::Cop::Lint::RedundantWithIndex
Checks for redundant ‘with_index`.
@example
# bad ary.each_with_index do |v| v end # good ary.each do |v| v end # bad ary.each.with_index do |v| v end # good ary.each do |v| v end
Constants
- MSG_EACH_WITH_INDEX
- MSG_WITH_INDEX
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 36 def on_block(node) return unless (send = redundant_with_index?(node)) range = with_index_range(send) add_offense(range, message: message(send)) do |corrector| if send.method?(:each_with_index) corrector.replace(send.loc.selector, 'each') else corrector.remove(range) corrector.remove(send.loc.dot) end end end
Also aliased as: on_numblock
Private Instance Methods
message(node)
click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 66 def message(node) if node.method?(:each_with_index) MSG_EACH_WITH_INDEX else MSG_WITH_INDEX end end
with_index_range(send)
click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 74 def with_index_range(send) range_between(send.loc.selector.begin_pos, send.loc.expression.end_pos) end