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.
Recent comments
1 year 23 weeks ago
1 year 23 weeks ago
1 year 25 weeks ago
1 year 27 weeks ago
1 year 42 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 46 weeks ago
1 year 48 weeks ago