Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Style/EachForSimpleLoop #3018

Closed
deivid-rodriguez opened this issue Apr 7, 2016 · 4 comments
Closed

Feature Request: Style/EachForSimpleLoop #3018

deivid-rodriguez opened this issue Apr 7, 2016 · 4 comments

Comments

@deivid-rodriguez
Copy link
Contributor

I'd like RuboCop to detect and autocorrect things like:

(1..8).each do |_n|
   puts "Do something that does not use the block parameter..."
   # ...
end

The preferred style would be

8.times do
  puts "Do something that does not use the block parameter..."
  # ...
end 

I guess it could be generalized to

collection.each do |_item|
  puts "Do something that does not use `_item` or mutate `collection`"
  # ...
end

Autocorrected the same way.

Does this sound like a good idea?

@Drenmi
Copy link
Collaborator

Drenmi commented Apr 7, 2016

Hi @deivid-rodriguez!

There's one called Lint/UnusedBlockArgument. Is this something like what you're looking for? It doesn't have auto-correct, though.

@deivid-rodriguez
Copy link
Contributor Author

deivid-rodriguez commented Apr 7, 2016

Hi @Drenmi! No, it's not that. The code I'm talking about has already been corrected by the cop you mention.

I'm looking for a smarter cop that detects that you're using things like (1..10).each { |n| ... } when you could use 10.times { ... }. It could reuse the Lint/UnusedBlockArgument though since a necessary condition for this replacement to make sense is that the block argument is unused.

@alexdowad
Copy link
Contributor

a necessary condition for this replacement to make sense is that the block argument is unused

Actually, it's not a necessary condition, because times also yields numbers from 0 up to N. But many people might find that less readable.

@deivid-rodriguez
Copy link
Contributor Author

Right, I find the times version more readable only when the iteration index is unused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants