Method: Enumerable#max_by

Defined in:
enum.c

#max_by {|obj| ... } ⇒ Object #max_byObject

Returns the object in enum that gives the maximum value from the given block.

If no block is given, an enumerator is returned instead.

a = %w(albatross dog horse)
a.max_by { |x| x.length }   #=> "albatross"

Overloads:

  • #max_by {|obj| ... } ⇒ Object

    Yields:

    • (obj)

    Returns:



1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
# File 'enum.c', line 1546

static VALUE
enum_max_by(VALUE obj)
{
    NODE *memo;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    memo = NEW_MEMO(Qundef, Qnil, 0);
    rb_block_call(obj, id_each, 0, 0, max_by_i, (VALUE)memo);
    return memo->u2.value;
}