Module: Enumerable

Defined in:
lib/geotree/tools.rb

Overview

Extensions to the Enumerable module

Instance Method Summary collapse

Instance Method Details

#max_with_index {|function| ... } ⇒ Object

Calculate a value for each item, and return the item with the highest value, its index, and the value.

Yield Parameters:

  • function

    to calculate value of an object, given that object as a parameter

Returns:

  • the triple [object, index, value] reflecting the maximum value, or nil if there were no items



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/geotree/tools.rb', line 160

def max_with_index 
  
  best = nil
  
  each_with_index do |obj,ind|
    sc = yield(obj)
    if !best || best[2] < sc
      best = [obj,ind,sc]
    end
  end
  best
end