Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#min

array.min → element
array.min { |a, b| ... } → element
array.min(n) → new_array
array.min(n) { |a, b| ... } → new_array

Returns one of the following:

  • The minimum-valued element from self.

  • A new Array of minimum-valued elements selected from self.

When no block is given, each element in self must respond to method <=> with an Integer.

With no argument and no block, returns the element in self having the minimum value per method <=>:

Example
[0, 1, 2].min # => 0

With Integer argument n and no block, returns a new Array with at most n elements, in ascending order per method <=>:

Example
[0, 1, 2, 3].min(3) # => [0, 1, 2]
[0, 1, 2, 3].min(6) # => [0, 1, 2, 3]

When a block is given, the block must return an Integer.

With a block and no argument, calls the block self.size-1 times to compare elements; returns the element having the minimum value per the block:

Example
['0', '00', '000'].min { |a, b| a.size <=> b.size } # => "0"

With an argument n and a block, returns a new Array with at most n elements, in ascending order per the block:

Example
[0, 1, 2, 3].min(3) # => [0, 1, 2]
[0, 1, 2, 3].min(6) # => [0, 1, 2, 3]
See also #max #minmax #map! #none? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1