public Method

Enumerable.max

enum.max                    obj
enum.max {|a,b| block }     obj

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

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

Source Code

/*
*  call-seq:
*     enum.max                   => obj
*     enum.max {|a,b| block }    => obj
*  
*  Returns the object in _enum_ with the maximum value. The
*  first form assumes all objects implement <code>Comparable</code>;
*  the second uses the block to return <em>a <=> b</em>.
*     
*     a = %w(albatross dog horse)
*     a.max                                  #=> "horse"
*     a.max {|a,b| a.length <=> b.length }   #=> "albatross"
*/  

static VALUE
enum_max(obj)
   VALUE obj;
{
   VALUE result = Qundef;

   rb_iterate(rb_each, obj, rb_block_given_p() ? max_ii : max_i, (VALUE)&result);
   if (result == Qundef) return Qnil;
   return result;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.