I want to use min(5,10)
, or Math.max(4,7)
. Are there functions to this effect in Ruby?
Ruby – How to find a min/max with Ruby
enumerableruby
Related Topic
- Ruby – the difference between include and require in Ruby
- Ruby-on-rails – How to understand nil vs. empty vs. blank in Ruby
- Ruby – How to write a switch statement in Ruby
- Ruby – How to check if a value exists in an array in Ruby
- Ruby-on-rails – How to get the current absolute URL in Ruby on Rails
- Ruby – Equivalent of “continue” in Ruby
- Ruby – attr_accessor in Ruby
- Ruby: How to install a specific version of a ruby gem
Best Answer
You can do
or
They come from the Enumerable module, so anything that includes
Enumerable
will have those methods available.v2.4 introduces own
Array#min
andArray#max
, which are way faster than Enumerable's methods because they skip calling#each
.@nicholasklick mentions another option,
Enumerable#minmax
, but this time returning an array of[min, max]
.