-
Notifications
You must be signed in to change notification settings - Fork 133
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
Refactor the interesting_ancestors_of
method in the Mixins listener
#967
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I just have a suggestion for further shortening.
lib/tapioca/gem/listeners/mixins.rb
Outdated
inherited_ancestors = Set.new.compare_by_identity | ||
inherited_ancestors.merge(inherited_ancestors_of(constant)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do all of this on a single line:
inherited_ancestors = Set.new.compare_by_identity | |
inherited_ancestors.merge(inherited_ancestors_of(constant)) | |
inherited_ancestors = Set.new.compare_by_identity.merge(inherited_ancestors_of(constant)) |
lib/tapioca/gem/listeners/mixins.rb
Outdated
ancestors = Set.new.compare_by_identity | ||
ancestors.merge(ancestors_of(constant)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
ancestors = Set.new.compare_by_identity | |
ancestors.merge(ancestors_of(constant)) | |
ancestors = Set.new.compare_by_identity.merge(ancestors_of(constant)) |
Co-authored-by: Ufuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
5a26c12
to
8414af4
Compare
@paracycle beautiful! Implemented your one-liners. |
interesting_ancestors_of
method in the Mixins listener
Motivation
Use
Set#compare_by_identity
to make the logic of theinteresting_ancestors_of
method easier to understand.Implementation
The old implementation worked as follows:
inherited_ancestors
as a new Set, and add all the object_ids of the modules ininherited_ancestors_of(constant)
constant.ancestors
and reject any whose object_id is already ininherited_ancestors_ids
The new implementation is much more straightforward:
inherited_ancestors
as a new Set withcompare_by_identity
inherited_ancestors_of(constant)
to this setancestors
as a new Set withcompare_by_identity
ancestors_of(constant)
to this setinherited_ancestors
fromancestors
and return the result as an arrayTests
Behavior should not change. Tests are still green.