class RuboCop::Cop::Style::EachForSimpleLoop
This cop checks for loops which iterate a constant number of times, using a Range literal and `#each`. This can be done more readably using `Integer#times`.
This check only applies if the block takes no parameters.
@example
@bad (1..5).each { } @good 5.times { }
@example
@bad (0...10).each {} @good 10.times {}
Constants
- MSG
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/style/each_for_simple_loop.rb, line 28 def on_block(node) return unless offending_each_range(node) send_node = node.send_node range = send_node.receiver.source_range.join(send_node.loc.selector) add_offense(node, location: range) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/each_for_simple_loop.rb, line 40 def autocorrect(node) lambda do |corrector| range_type, min, max = offending_each_range(node) max += 1 if range_type == :irange corrector.replace(node.children.first.source_range, "#{max - min}.times") end end