479

I want to use min(5,10), or Math.max(4,7). Are there functions to this effect in Ruby?

5 Answers 5

835

.min

[5, 10].min

.max

[4, 7].max

They come from the Enumerable module, so anything that includes Enumerable will have those methods available.

v2.4 introduces own Array#min and Array#max, which are way faster than Enumerable's methods because they skip calling #each.

.minmax

@nicholasklick mentions another option, Enumerable#minmax, but this time returning an array of [min, max].

[4, 5, 7, 10].minmax
#=> [4, 10]
5
  • 1
    Don't forget to sanitize the array first by removing nil values, or values that cannot be compared Jun 15, 2015 at 22:02
  • 10
    Ruby is mainly for the programmer not for the computer. In Matz's words "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language." That's from the Wikipedia page on Ruby. Nov 2, 2015 at 23:42
  • @aaron-coding Well, helping the programmer to write inefficient code isn't exactly helping him. ;) But yes - I know in many cases these micro-optimizations don't matter. However, I think there is an underlying problem in Ruby encouraging inefficiency. Efficiency is not Ruby's greatest strength or design goal; it has other, more eloquent values. Oct 31, 2017 at 9:31
  • Am I wrong, or does @nicholasklick not exist any more? I wanted to link him, but weren't able to find him.
    – Cadoiz
    Dec 16, 2022 at 7:23
  • 1
    The existence of .minmax is yet another reason why I love Ruby. May 24, 2023 at 19:26
59

You can use

[5,10].min 

or

[4,7].max

It's a method for Arrays.

2
  • 24
    Technically it's a method for Enumerables, not Arrays.
    – user229044
    Dec 12, 2013 at 22:36
  • 6
    It's a method for Arrays with superior performance over Enumerable's since v2.4 Jul 3, 2018 at 6:13
32

If you need to find the max/min of a hash, you can use #max_by or #min_by

people = {'joe' => 21, 'bill' => 35, 'sally' => 24}

people.min_by { |name, age| age } #=> ["joe", 21]
people.max_by { |name, age| age } #=> ["bill", 35]
32

All those results generate garbage in a zealous attempt to handle more than two arguments. I'd be curious to see how they perform compared to good 'ol:

def max (a,b)
  a>b ? a : b
end

which is, by-the-way, my official answer to your question.

3
21

In addition to the provided answers, if you want to convert Enumerable#max into a max method that can call a variable number or arguments, like in some other programming languages, you could write:

def max(*values)
 values.max
end

Output:

max(7, 1234, 9, -78, 156)
=> 1234

This abuses the properties of the splat operator to create an array object containing all the arguments provided, or an empty array object if no arguments were provided. In the latter case, the method will return nil, since calling Enumerable#max on an empty array object returns nil.

If you want to define this method on the Math module, this should do the trick:

module Math
 def self.max(*values)
  values.max
 end
end

Note that Enumerable.max is, at least, two times slower compared to the ternary operator (?:). See Dave Morse's answer for a simpler and faster method.

2
  • 1
    But isn't reopening standard classes and modules considered a bad practice? Aug 16, 2018 at 9:13
  • You could do a monkey patch only locally with the module keyword.
    – Cadoiz
    Dec 16, 2022 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.