sum = @products.inject(0){ |sum,item| sum += item['count'] }
@selected = @products.select { |item| (item['count']/sum) >= 0.05 }
I want to select each element from an array @productswhose property countis more than 5% of sum. @products- an array of hashes.
However, when I use this second line, @selected returns an empty array. Not finding any errors in |item|or in the array itself @products, I am inclined to believe that it has something to do with trying to use an external variable suminside the block .select. Can someone explain to me why @selectedit returns nothing?
source
share