Module: Enumerable

Defined in:
lib/core_ext/enumerable.rb

Overview

Extends Ruby’s own Enumrable module with method #max_by? for Ruby < 1.8.7

Author:

  • Sebastian Staudt

Since:

  • 0.6.0

Instance Method Summary collapse

Instance Method Details

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

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

Yields:

  • (obj)

    The block to call on each element in the enum

Yield Parameters:

  • obj (Object)

    A single object in the enum

Yield Returns:

  • (Comparable)

    A value that can be compared (+<=>+) with the values of the other objects in the enum

Since:

  • 0.6.0



21
22
23
# File 'lib/core_ext/enumerable.rb', line 21

def max_by(&block)
  max { |a , b| block.call(a) <=> block.call(b) }
end