From: Noah Easterly Date: 2007-08-07T01:30:10+09:00 Subject: Re: pulling specific dup. elements out of an array On Aug 6, 11:08 am, Stefan Rusterholz wrote: > Why do you want to remove the 0 elements for that? They won't influence > the result: > avg_kb = array.inject(0.0) { |sum,num| sum + num.to_f > > }/array.length/1024 but rejecting the 0 elements will change the length of the array, and change the resulting average. It depends on whether you want to generate the average across all values or the average across all non-zero values. with_zeroes = [ 0, 0, 0, 1.0 ] without_zeroes = with_zeroes.reject { |x| x == 0 } #=> [ 1.0 ] with_zeroes.inject { |a,b| a + b } / with_zeroes.length #=> 0.25 without_zeroes.inject { |a,b| a + b } / without_zeroes.length #=> 1.0