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