The beauty of transpose

That’s when I heard about this wonderful method called “transpose”

This method changed my life. Essentially it takes an array of arrays, and sorts them based on their index. Index zero in each array gets put into the first array, index one in the second, etc. If you’ve seen “#zip” this may look somewhat familiar, as it is pretty much zip in reverse.

This had huge implications for my project since Braille is written in three columns of three dots or 0s a piece. With this method I could sort them out according to row, and do what I pleased with them.

Since then I’ve found several occasions to use transpose. It has many uses when you have several similar elements saved inside of a nested array, or even a hash key with a value of an array or nested array, such as I had in my project.

These vague methods get overlooked far too often, by far too many developers, myself included. So my advice? Get in that ruby documentation and play around a little. There’s no telling what wonderful treasures you’ll come across!

Show your support

Clapping shows how much you appreciated Caleb Cowen’s story.