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 min…


This content originally appeared on DEV Community and was authored by Ahmad khattab

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]

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]

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] 

Hope you have a great day!. Happy Coding.

Links


This content originally appeared on DEV Community and was authored by Ahmad khattab


Print Share Comment Cite Upload Translate Updates
APA

Ahmad khattab | Sciencx (2021-10-28T20:04:26+00:00) Discoveries in Ruby(and Rails): Enumerable#minmax. Retrieved from https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/

MLA
" » Discoveries in Ruby(and Rails): Enumerable#minmax." Ahmad khattab | Sciencx - Thursday October 28, 2021, https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/
HARVARD
Ahmad khattab | Sciencx Thursday October 28, 2021 » Discoveries in Ruby(and Rails): Enumerable#minmax., viewed ,<https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/>
VANCOUVER
Ahmad khattab | Sciencx - » Discoveries in Ruby(and Rails): Enumerable#minmax. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/
CHICAGO
" » Discoveries in Ruby(and Rails): Enumerable#minmax." Ahmad khattab | Sciencx - Accessed . https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/
IEEE
" » Discoveries in Ruby(and Rails): Enumerable#minmax." Ahmad khattab | Sciencx [Online]. Available: https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/. [Accessed: ]
rf:citation
» Discoveries in Ruby(and Rails): Enumerable#minmax | Ahmad khattab | Sciencx | https://www.scien.cx/2021/10/28/discoveries-in-rubyand-rails-enumerableminmax/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.