class RuboCop::Cop::Lint::RedundantWithObject
Checks for redundant ‘with_object`.
@example
# bad ary.each_with_object([]) do |v| v end # good ary.each do |v| v end # bad ary.each.with_object([]) do |v| v end # good ary.each do |v| v end
Constants
- MSG_EACH_WITH_OBJECT
- MSG_WITH_OBJECT
Public Instance Methods
Source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 36 def on_block(node) return unless (send = redundant_with_object?(node)) range = with_object_range(send) add_offense(range, message: message(send)) do |corrector| if send.method?(:each_with_object) corrector.replace(range, 'each') else corrector.remove(range) corrector.remove(send.loc.dot) end end end
Also aliased as: on_numblock, on_itblock
Private Instance Methods
Source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 68 def message(node) if node.method?(:each_with_object) MSG_EACH_WITH_OBJECT else MSG_WITH_OBJECT end end
Source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 76 def with_object_range(send) range_between(send.loc.selector.begin_pos, send.source_range.end_pos) end