Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/e9_crm/rails_extensions.rb

Instance Method Summary collapse

Instance Method Details

#averageObject

Simple helper for array calculating averages

Returns 0 if all array elements do not exist and respond to to_f

NOTE nil responds to to_f but not to +, which would throw an

exception on sum if nil was allowed in the array


13
14
15
16
# File 'lib/e9_crm/rails_extensions.rb', line 13

def average
  return 0 unless length > 0 && all? {|n| n && n.respond_to?(:to_f) }
  sum / length
end