1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Find The Min And Max With A Single Call as a ruby til

This commit is contained in:
jbranchaud
2020-12-13 17:22:36 -06:00
parent 304e09afcd
commit 0cf338c7ea
2 changed files with 27 additions and 1 deletions

View File

@@ -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)

View File

@@ -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]
```