class RuboCop::Cop::Style::For
end
end
puts n
for n in [1, 2, 3] do
def foo
# good
end
end
puts n
[1, 2, 3].each do |n|
def foo
# bad
@example EnforcedStyle: for
end
end
puts n
[1, 2, 3].each do |n|
def foo
# good
end
end
puts n
for n in [1, 2, 3] do
def foo
# bad
@example EnforcedStyle: each (default)
allowed, however.
parameter. An each call with a block on a single line is always
preferred alternative is set in the EnforcedStyle configuration
This cop looks for uses of the for keyword, or each method. The
def incorrect_style_detected(method)
def incorrect_style_detected(method) end_pos = method.source_range.end_pos range = range_between(end_pos - EACH_LENGTH, end_pos) msg = 'Prefer `for` over `each`.' add_offense(range, location: range, message: msg) do opposite_style_detected end end
def on_block(node)
def on_block(node) return if node.single_line? return unless node.send_node.method?(:each) && !node.send_node.arguments? if style == :for incorrect_style_detected(node.send_node) else correct_style_detected end end
def on_for(node)
def on_for(node) if style == :each msg = 'Prefer `each` over `for`.' add_offense(node, location: :keyword, message: msg) do opposite_style_detected end else correct_style_detected end end