From 0cf338c7ea30f240ea4856ad426653f7b2aaa940 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 13 Dec 2020 17:22:36 -0600 Subject: [PATCH] Add Find The Min And Max With A Single Call as a ruby til --- README.md | 3 ++- ...find-the-min-and-max-with-a-single-call.md | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ruby/find-the-min-and-max-with-a-single-call.md diff --git a/README.md b/README.md index d86f829..61ddbfd 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_973 TILs and counting..._ +_974 TILs and counting..._ --- @@ -785,6 +785,7 @@ _973 TILs and counting..._ - [Expect A Method To Be Called And Actually Call It](ruby/expect-a-method-to-be-called-and-actually-call-it.md) - [FactoryGirl Sequences](ruby/factory-girl-sequences.md) - [Fail](ruby/fail.md) +- [Find The Min And Max With A Single Call](ruby/find-the-min-and-max-with-a-single-call.md) - [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md) - [Generate A Signed JWT Token](ruby/generate-a-signed-jwt-token.md) - [Generate Ruby Version And Gemset Files With RVM](ruby/generate-ruby-version-and-gemset-files-with-rvm.md) diff --git a/ruby/find-the-min-and-max-with-a-single-call.md b/ruby/find-the-min-and-max-with-a-single-call.md new file mode 100644 index 0000000..731643f --- /dev/null +++ b/ruby/find-the-min-and-max-with-a-single-call.md @@ -0,0 +1,25 @@ +# Find The Min And Max With A Single Call + +Ruby's Enumerable comes with the `#min` and `#max` methods for finding, +respectively, the minimum and maximum value in the target collection. + +If you wanted to find both the min and the max of the same collection, you +could call them one after another. + +```ruby +list = [3,7,4,15,9,1,2] + +list.min +#=> 1 +list.max +#=> 15 +``` + +Ruby's Enumerable also supports a slightly more efficient way -- it finds both +at the same time when you call +[`#minmax`](https://apidock.com/ruby/Enumerable/minmax). + +```ruby +list = [3,7,4,15,9,1,2] +#=> [1,15] +```