Array#max
Added in v2.4 · Updated March 13, 2026 · Array Methods
ruby array max min
Array#max, Array#min, and Array#minmax return the largest and smallest elements in an array. These methods were added directly to the Array class in Ruby 2.4, making them significantly faster than their Enumerable counterparts since they avoid the overhead of iterating through #each.
Syntax
arr.max # returns maximum element
arr.max(n) # returns array of n largest elements
arr.max { |a,b| } # returns maximum using block comparison
arr.min # returns minimum element
arr.min(n) # returns array of n smallest elements
arr.min { |a,b| } # returns minimum using block comparison
arr.minmax # returns [min, max] as two-element array
arr.minmax { |a,b| } # returns [min, max] using block comparison
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n | Integer | nil | When provided, returns an array of the n largest/smallest elements |
All methods also accept an optional block that defines custom comparison logic, overriding the default <=> operator.
Examples
Basic usage with numbers
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
# Find the maximum
numbers.max
# => 9
# Find the minimum
numbers.min
# => 1
# Get both at once
numbers.minmax
# => [1, 9]
Get multiple extremes
scores = [95, 87, 92, 88, 79, 91, 85, 90]
# Get top 3 scores
scores.max(3)
# => [95, 92, 91]
# Get bottom 3 scores
scores.min(3)
# => [79, 85, 87]
With strings (alphabetical comparison)
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
fruits.max
# => "elderberry"
fruits.min
# => "apple"
fruits.minmax
# => ["apple", "elderberry"]
Using a block for custom comparison
words = ["cat", "elephant", "bat", "ant"]
# Compare by length
words.max { |a, b| a.length <=> b.length }
# => "elephant"
words.min { |a, b| a.length <=> b.length }
# => "bat"
# Get extremes by length
words.minmax { |a, b| a.length <=> b.length }
# => ["bat", "elephant"]
With empty arrays
empty = []
empty.max
# => nil
empty.min
# => nil
empty.minmax
# => [nil, nil]
empty.max(3)
# => []
empty.min(3)
# => []
Finding extremes in hashes by value
prices = { apple: 1.50, banana: 0.75, orange: 2.00, mango: 3.50 }
# Convert to array of [key, value] pairs
items = prices.to_a
# Most expensive
items.max { |a, b| a[1] <=> b[1] }
# => [:mango, 3.50]
# Cheapest
items.min { |a, b| a[1] <=> b[1] }
# => [:banana, 0.75]
Common Patterns
Check for highest/lowest values
# Simple case: get extremes
data = [23, 45, 12, 67, 34]
highest = data.max
lowest = data.min
# More explicit with interpolation
puts "Range: #{lowest} to #{highest}"
# => Range: 12 to 67
Avoid multiple iterations
# Bad: iterates twice
max_value = data.max
min_value = data.min
# Good: iterates once
min_val, max_val = data.minmax
This matters with large datasets where the difference between one pass and two passes is noticeable.
Using with sort
data = [5, 2, 8, 1, 9]
# These are equivalent but max/min is faster
data.max # => 9
data.sort.last # => 9
data.min # => 1
data.sort.first # => 1
Performance Note
Array’s native #max and #min (Ruby 2.4+) are 10x faster than Enumerable’s versions because they skip the call to #each. If you’re working with Arrays specifically, always prefer these over the Enumerable methods.
Error Cases
# With n > array length, returns all elements (sorted)
[1, 2].max(10)
# => [2, 1]
# With n = 0, returns empty array
[1, 2].max(0)
# => []
# nil in array works fine
[1, nil, 3].max
# => 3
# Comparison must be defined
[Object.new, Object.new].max
# => ArgumentError: comparison of Object with Object failed