[Feature #19830] Allow Array#transpose
to take an optional element size argument
#8167
+41
−12
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.
One benefit of supplying an initial value to
Enumerable#inject
is that it avoids an annoying edge case when the collection is empty:A similar edge case exists for
Array#transpose
:Although no explicit
nil
is produced here, the subtle problem is that the caller may assume that the result array contains arrays, and that assumption leads tonil
s in the empty case:If we allow
Array#transpose
to take an optional argument specifying the size of the result array, we can use this to always return an array of the correct size:By avoiding an unexpectedly empty result array, we also avoid unexpected downstream
nil
s:This PR adds an optional argument to
Array#transpose
to support the above usage.Something similar was requested eleven years ago in feature #6852. I believe this PR addresses the problem expressed in that issue without compromising backward compatibility with existing callers of
#transpose
.