Today I Learned

hashrocket A Hashrocket project

Get Array's Min and Max with minmax

In ruby, you can get the minimum value in an array with #min and the maximum value with #max.

But sometimes you need both the min and max, and it feels a waste to make two separate calls. Turns out you can get both in one with #minmax - which will return a two element array with the min and max values.

pry(main)> [1, 2, 3].minmax
# => [1, 3]

You can also pass it a block for custom ordering criteria.

Docs

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.