/ rails Public
Array.second_to_last and Array.third_to_last access methods #23583
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.
Reading @dhh's Rails Doctrine essay got me thinking a lot about Rails'
fifthaccessor methods, which I (like DHH) love so much.
One of the things that has occurred to me about once every month or two since I began working with Rails (back in 2008) is that it'd be nice to have similarly natural ways of accessing the end of an array or relation. We have
last, but it ends there. It's slightly jarring (for me anyway) to go from
So, here's a proposal to add
antepenultimateas well. Partly for the delight factor, partly because I think it would actually be useful. (At least, it would to me.)
third_to_lastare more natural, we can easily use those instead, or make them aliases.
This is my first Rails PR, so I've tried to include everything (tests, documentation), but I'm happy to make alterations if I've missed anything.