Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This cop looks for pointless uses of
Integer#times
and replaces or removes them as necessary (I found some of these in my codebase at work, sadly). When the integer is <= 0, the block will never be yielded to, so the entire thing can be removed. When the integer is 1, the block will only ever yield once, so the1.times
call can (generally) be replaced with the block body.Auto-correction is enabled in most cases, but because
Integer#times
returns its receiver (and the replacement won't), I've marked it as unsafe.The cop will remove
0.times
and-5.times
(etc.) calls, since they will never do anything. The cop will replace1.times
with its block, and if the block arg is used at all, replace it with 1.Auto-correction will not be attempted in a couple more complex cases, such as mutating the block arg within the block, or calling
Integer#times
not on its own line. See the tests for more details.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.