Sha256: d5df0f1f129807529921bb0aec66f38210aa75c08c55d3eee6f2bba9acd94f51

Contents?: true

Size: 372 Bytes

Versions: 8

Compression:

Stored size: 372 Bytes

Contents

module Array::Average
  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
end

class Array
  include Average
end

module Array::Average::Etest
  def test_avg
    assert_equal(1, [ 1 ].avg)
    assert_equal(2, [ 1, 3 ].avg)
    assert_equal(0, [ ].avg)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vex-0.3.3 lib/vex/base/array/avg.rb
vex-0.2.9 lib/vex/base/array/avg.rb
vex-0.2.8 lib/vex/base/array/avg.rb
vex-0.2.7 lib/vex/base/array/avg.rb
vex-0.2.6 lib/vex/base/array/avg.rb
vex-0.2.5 lib/vex/base/array/avg.rb
vex-0.2.2 lib/vex/base/array/avg.rb
vex-0.2.1 lib/vex/base/array/avg.rb