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

[Fixes #22] AutoConstToSet builds Set variants of constants automatically #57

Closed
wants to merge 2 commits into from

Conversation

marcandre
Copy link
Contributor

Here's another approach to deal with the fact that calling some_array.include? is a waste of CPU cycles.

If a module extends AutoConstToSet, then SOME_CONSTANT_SET will be a set created automatically from SOME_CONSTANT.

class Foo
 extend AutoConstToSet
 WORDS = %w[hello world].freeze
end
Foo::WORDS_SET # => Set['hello', 'world'].freeze

This has the advantage that both the Array and the Set versions are available, without too much verbosity.

It requires the rubyist to call SOME_CONSTANT_SET.include? instead of SOME_CONSTANT.include? but we could even have an internal cop for this.

@marcandre marcandre added this to the 1.0.0 milestone Jul 7, 2020
@marcandre
Copy link
Contributor Author

@rubocop-hq/rubocop-core how does this solution look?

@bbatsov
Copy link
Contributor

bbatsov commented Jul 12, 2020

My only concern is that this behavior is not particularly obvious and probably people will be a bit confused by it. A cop might help, though.

On the bright side - it is the least invasive option we've discussed so far, although I personally prefer a more explicit approach even if we have to write more code.

@marcandre
Copy link
Contributor Author

If I make the ..._SET constants private, then it should be possible to revert this or go to any other solution without any impact...

Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

None yet

3 participants