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
enum.c
View on GitHub ↗
static VALUE
enum_min(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
struct min_t *m = NEW_CMP_OPT_MEMO(struct min_t, memo);
VALUE result;
VALUE num;
if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
return rb_nmin_run(obj, num, 0, 0, 0);
m->min = Qundef;
m->cmp_opt.opt_methods = 0;
m->cmp_opt.opt_inited = 0;
if (rb_block_given_p()) {
rb_block_call(obj, id_each, 0, 0, min_ii, memo);
}
else {
rb_block_call(obj, id_each, 0, 0, min_i, memo);
}
result = m->min;
if (result == Qundef) return Qnil;
return result;
}
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]