Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#min

() -> Elem
() { (Elem a, Elem b) -> ::Integer? } -> Elem
(::int n) -> ::Array[Elem]
(::int n) { (Elem a, Elem b) -> ::Integer? } -> ::Array[Elem]
min → element
min(count) → new_array
min {|a, b| ... } → element
min(count) {|a, b| ... } → new_array

Returns one of the following:

  • The minimum-valued element from self.

  • A new array of minimum-valued elements from self.

Does not modify self.

With no block given, each element in self must respond to method <=> with a numeric.

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

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

With non-negative numeric argument count and no block, returns a new array with at most count elements, in ascending order, per method <=>:

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

With a block given, the block must return a numeric.

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', '', '000', '00'].min {|a, b| a.size <=> b.size }
# => ""

With non-negative numeric argument count and a block, returns a new array with at most count elements, in ascending order, per the block:

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

Related: see Methods for Fetching.

See also #max #minmax #map! #none? also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0