On Sun, Jan 23, 2011 at 10:31 PM, redstun <redstun / gmail.com> wrote:
> Enumerable.inject is pretty cool, but it treat the elements as if they have
> the same weight when doing the calculation, in my case I need to apply a
> different weight based on the index of each element, therefore I also need
> the index being provided as one more block parameter.
> enum.find_index might help, but I think there should be a more direct way to
> get the index.
> In short, like for enum.each_entry, there's each_with_index,
> similarly, for enum.inject, is it possible to have enum.inject_with_index {
> |memo, obj, index| ... } ?

how about,

  (0..5).each.with_index.inject(0){|sum,(i,j)|  sum+j}
  #=> 15

  (0..5).each.with_index.inject(0){|sum,(i,j)|  sum+i*j}
  #=> 55

best regards -botp