read

Ruby inject method is useful for combining an element inside enumerable, with binary operation and initial value.

For example inject method useful for accumulation in one dimensional array :

[1,2,3].inject(0){ |initial_variable, element| element + initial_variable }

from above operation, 0 in inject argument is initial value of initial_variable if you forget to specify an argument in inject method by default first element in enumerable will be used as initial value.

From above example inject method will accumulate all element inside array / collection, which is :

[1,2,3].inject(0){ |initial_variable, element| element + initial_variable} # => same as 1+2+3 = 6

Another example :

[2,4,6,8].inject(10){ |initial, element| initial * element } # => 3840

Well, from above example you can simplify the operation process by giving operator symbol like :+, :*, :/

[1,2,3].inject(:+) # => 6
[2,4,6].inject(:*) # => 48

Another awesomeness inject method is you can build hash key value from multi dimensional 2xN array, for example :

[[:a,1],[:b,2],[:c,3]].inject({}){|init,elem| init.merge(elem.first => elem.last) }
# => {:a=>1, :b=>2, :c=>3}

Blog Logo

Agung Prasetyo


Published

Image

devblog of @sgt_mactavish

Back to Overview