class RubbyCop::Cop::Style::For
This cop looks for uses of the for keyword, or each method. The preferred alternative is set in the EnforcedStyle configuration parameter. An each call with a block on a single line is always allowed, however.
Constants
- EACH_LENGTH
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubbycop/cop/style/for.rb, line 24 def on_block(node) return if block_length(node).zero? method, _args, _body = *node return unless method.send_type? && method.method?(:each) && !method.arguments? if style == :for incorrect_style_detected(method) else correct_style_detected end end
on_for(node)
click to toggle source
# File lib/rubbycop/cop/style/for.rb, line 14 def on_for(node) if style == :each add_offense(node, :keyword, 'Prefer `each` over `for`.') do opposite_style_detected end else correct_style_detected end end
Private Instance Methods
incorrect_style_detected(method)
click to toggle source
# File lib/rubbycop/cop/style/for.rb, line 40 def incorrect_style_detected(method) end_pos = method.source_range.end_pos range = range_between(end_pos - EACH_LENGTH, end_pos) add_offense(range, range, 'Prefer `for` over `each`.') do opposite_style_detected end end