09 Feb 2010

Inject for array

Inject is awesome. I was going through comments of this blog and learned something new today.

Given below is a case where I need to add all the numbers. One way to solve would be something like this.

ar = [[:a, 1], [:b, 2], [:c, 3]]
c = ar.inject(0) do |sum, record|
  sum += record[1]
end
puts c #=> 6

Here is another way to solve the same problem.

ar = [[:a, 1], [:b, 2], [:c, 3]]
d = ar.inject(0) do |sum, (first, second)|
  sum += second
end
puts d

The second parameter in the block is split based on pattern. If the inner array has three elements then all the three elements can be split based on pattern.

ar = [[:a, 1, 11], [:b, 2, 22], [:c, 3, 33]]
d = ar.inject(0) do |sum, (first, second, third)|
  sum += third
end
puts d #=> 66