class RuboCop::Cop::Lint::RedundantWithObject
end
v
ary.each do |v|
# good
end
v
ary.each.with_object([]) do |v|
# bad
end
v
ary.each do |v|
# good
end
v
ary.each_with_object([]) do |v|
# bad
@example
This cop checks for redundant ‘with_object`.
def autocorrect(node)
def autocorrect(node) lambda do |corrector| redundant_with_object?(node) do |send| if send.method_name == :each_with_object corrector.replace(with_object_range(send), 'each') else corrector.remove(with_object_range(send)) corrector.remove(send.loc.dot) end end end end
def message(node)
def message(node) if node.method_name == :each_with_object MSG_EACH_WITH_OBJECT else MSG_WITH_OBJECT end end
def on_block(node)
def on_block(node) redundant_with_object?(node) do |send| add_offense(node, location: with_object_range(send)) end end
def with_object_range(send)
def with_object_range(send) range_between( send.loc.selector.begin_pos, send.loc.expression.end_pos ) end