Method: Array#average

Defined in:
lib/commonthread/monkey_patches.rb

#average(&block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/commonthread/monkey_patches.rb', line 93

def average(&block)
  sum = 0
  items = 0

  self.each do |n|
    current_value = yield(n)
    sum += current_value and items += 1 if current_value
  end

  if items > 0
    sum.to_f / items.to_f
  else
    nil
  end
end