Module: Array::Average

Included in:
Array
Defined in:
lib/vex/base/array/avg.rb

Defined Under Namespace

Modules: Etest

Instance Method Summary collapse

Instance Method Details

#avgObject



2
3
4
5
6
7
8
9
10
# File 'lib/vex/base/array/avg.rb', line 2

def avg
  return 0 if empty?

  sum = inject(0) { |s, i| s + i }

  avg = sum / length
  return avg if length * avg == sum
  sum * 1.0 / length
end