class RuboCop::Cop::Style::EachForSimpleLoop
10.times {}
# good
(0…10).each {}
# bad
@example
5.times { }
# good
(1..5).each { }
# bad
@example
This check only applies if the block takes no parameters.
‘Integer#times`.
using a Range literal and `#each`. This can be done more readably using
Checks for loops which iterate a constant number of times,
def offending?(node)
def offending?(node) each_range_with_zero_origin?(node) || each_range_without_block_argument?(node) end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return unless offending?(node) send_node = node.send_node range = send_node.receiver.source_range.join(send_node.loc.selector) add_offense(range) do |corrector| range_type, min, max = each_range(node) max += 1 if range_type == :irange corrector.replace(node.send_node, "#{max - min}.times") end end