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

Enumerable#min

enum.min → obj
enum.min { |a, b| block } → obj
enum.min(n) → array
enum.min(n) { |a, b| block } → array

Returns the object in enum with the minimum value. The first form assumes all objects implement <=>; the second uses the block to return a <=> b.

Example
a = %w(albatross dog horse)
a.min                                   #=> "albatross"
a.min { |a, b| a.length <=> b.length }  #=> "dog"

If the n argument is given, minimum n elements are returned as a sorted array.

Example
a = %w[albatross dog horse]
a.min(2)                                  #=> ["albatross", "dog"]
a.min(2) {|a, b| a.length <=> b.length }  #=> ["dog", "horse"]
[5, 1, 3, 4, 2].min(3)                    #=> [1, 2, 3]
See also #member? #min_by #max_by #minmax also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1