Refactor Enumerable#map
to delegate to #map_with_index
#15210
Merged
+3
−3
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.
#map
andmap_with_index
are identical except that the latter also yields an index counter.But this counter is entirely optional and can be omitted from the block. It's possible to call
#map_with_index
with exactly the same signature as#map
.The implementation of both methods is also pretty much identical, in
Enumerable
as well as in any including type.Of course,
map_with_index
has a counter and yields its value. But LLVM optimization happily removes it when unused.So I think it makes sense to implement
Enumerable#map
by delegating to#map_with_index
. This embodies the close connection between these two methods.As a result, including types only need to override
#map_with_index
with a custom implementation and#map
will follow suit.Including types may still override
#map
with a custom implementation, of course.