Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional summarizing via inject

How to get the index of item in:

my_array.inject {|rs,item| rs += item}

I need to summarize all except the i-th element.

like image 980
gmile Avatar asked Dec 22 '22 08:12

gmile


1 Answers

Just summarize over the indices.

(0...a.size).inject(0) { |sum, index| if index != (i - 1) then sum + my_array[i] else sum }
like image 59
Dario Avatar answered Dec 28 '22 09:12

Dario