From: Robert Klemme Date: 2007-08-06T23:44:15+09:00 Subject: Re: pulling specific dup. elements out of an array 2007/8/4, Jon Hawkins : > Chris wrote: > > why didn't reject work? > > > >>> array = ['234234','04593','4098234','0','0','0'] > > => ["234234", "04593", "4098234", "0", "0", "0"] > >>> array.reject {|e| e == '0' } > > => ["234234", "04593", "4098234"] > > well lemme further explain what im attempting to do and why i couldnt > get reject to work right, i need to delete all the 0's in the array then > add up those non-zero numbers. > with: > > array.inject(0) {|num, i| num + i}/array.length/1024 #1024 = > kilobytes convert > > so if theres a way to shove reject into that then lemme know ^^ Just a small remark: you do not have numbers in your array but strings. I assume you want to using numeric addition and not string concatenation. In that case I'd do: irb(main):001:0> array = ['234234','04593','4098234','0','0','0'] => ["234234", "04593", "4098234", "0", "0", "0"] irb(main):002:0> nums = array.inject([]) {|a,n| a << n.to_i unless n == "0"; a} => [234234, 4593, 4098234] irb(main):003:0> avg = nums.inject(0) {|s,n| s+n}.to_f / nums.size / 1024 => 1411.8037109375 Alternative if you prefer a single pass: irb(main):018:0> sum,count = array.inject([0,0]) {|(s,c),n| n == "0" ? [s,c] : [s+n.to_i,c+1]} => [4337061, 3] irb(main):019:0> avg = sum.to_f / count / 1024 => 1411.8037109375 Kind regards robert