Ruby

Get the Maximum and Minimum Values in a Ruby Array

In this tutorial, we will learn how to use the various methods from the Array Class in the Ruby programming language. The Array class has access to the Enumerable module which provides a set of methods to interact with the Array elements.

Get the Maximum Value in an Array in Ruby

We can use the Array#max method to find the maximum value from a given set of elements. The method returns the element and not the position of the maximum element in the array.

The method also allows us to return more than one value by specifying the number of values to fetch as an argument.

We can express the function syntax as follows:

array.max()

 
To return more than one value, we can use the syntax as follows:

array.max([n])

 
Where n is an optional parameter that determines the number of elements which are returned by the function. This returns the values in descending order.

Function Return Value

The function returns an array which contains the maximum values that are specified in the n parameter. If n is not provided, the function returns a single value.

Example Function Usage

The following example demonstrates the function usage with an array of integer values:

irb(main):001:0> arr = [10,22,12,100,121,239]
=> [10, 22, 12, 100, 121, 239]
irb(main):002:0> arr.max
=> 239

 
The function returns the highest value from the input array in this case. To get the three highest values, we can pass the value of n as shown:

irb(main):003:0> arr.max(3)
=> [239, 121, 100]

 
In this case, the function returns an array with the three highest values in descending order.

We can also use the function to fetch the maximum value from an array of strings. An example demonstration is as follows:

irb(main):004:0> arr = ['MySQL', 'PostgreSQL', 'Redis', 'SQL Server']
=> ["MySQL", "PostgreSQL", "Redis", "SQL Server"]
irb(main):005:0> arr.max
=> "SQL Server"

 
In this case, the function sorts the string based on the characters of the input string. The higher the characters are in the alphabetical index, the higher the value.

This is a similar case for an array of characters, as demonstrated in the following:

irb(main):008:0> arr = ['a', 'b', 'c','z', 'q']
=> ["a", "b", "c", "z", "q"]
irb(main):009:0> arr.max
=> "z"

 
In this case, the function returns the character with the highest alphabetical index.

irb(main):010:0> arr.max(3)
=> ["z", "q", "c"]

 

Get the Minimum Value in an Array in Ruby

We can fetch the minimum value from an array using the Array#min() function. The function follows a similar syntax to the max function as shown in the following:

array.min()

 
To get more than one element from the array, use the following command:

array.min(n)

 
Where n refers to the number of elements with the highest or minimum values.

An example demonstration is as follows:

strings = ['hello', 'world']
ints = [1,2,3,4,5]
chars = ['a', 'b', 'x', 'c']
irb(main):018:0> strings.min
=> "hello"
irb(main):019:0> ints.min
=> 1
irb(main):020:0> chars.min
=> "a"

 
As we can see, the function returns the smallest value in a given array. In the case of characters and strings, the function sorts the values based on their positions in alphabetical order.

Conclusion

We explored how we can use the Array#max and Array#min functions to fetch the maximum and minimum values from an array in Ruby.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list