DEV Community

Ahmad khattab
Ahmad khattab

Posted on

3 2

Discoveries in Ruby(and Rails): Enumerable#minmax

Hi. I'm ahmed, a Rails enthusiastic and i use in my daily life as a developer. This will hopefully be a series in which i write on small(tiny-tips) i discover while coding Ruby.

Enumerable#minmax

Whenever you need to get the maximum and minimum value in any enumerable list. It returns a tuple which is an array of exactly two items(because tuples are not types in Ruby!). The first item being the min and the second being the max in minmax.

[1,2,3,4,5,6].minmax
# => [1,6]
Enter fullscreen mode Exit fullscreen mode

The list does not even have to be in any specific order, you can also use it with enumerables that are not ordered.

[6,1,5,10,9,0].minmax
# => [0,10]
Enter fullscreen mode Exit fullscreen mode

Get the farthest between a group of dates(with Rails extensions)

[3.days.from_now, 1.day.from_now, Date.today].minmax
#=> [Thu, 28 Oct 2021, Sun, 31 Oct 2021 15:00:38.694977000 CDT -05:00] 
Enter fullscreen mode Exit fullscreen mode

Hope you have a great day!. Happy Coding.

Links

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
antergos98 profile image
Vincent Bergeron

That's cool! That's why I love Ruby (and Rails). There's so many great functions in the language and in the framework itself

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️