Ruby Inject: How to manipulate your arrays with ease

ruby injectRuby is a powerful object-oriented language that has been around since the 90s. There have many iterations of the language since it was first introduced. Currently, Ruby 2.1.0 is the newest version available to programmers. If you’re new to Ruby, you can download the Ruby 2.1.0 source code from the official website for free and install it on your PC. You will find that Ruby has a wide variety of uses and is suited for most major platforms. It’s one of the best languages out there to develop applications in.

In this tutorial, we’re going to learn how the INJECT keyword works in Ruby. If you’re new to Ruby, you might want to learn how to write basic code in the Ruby first. It’s not too hard- Ruby is very similar to Java and C. If you’re familiar with programming in Java or C, you should be able to learn Ruby in no time.

Ruby Inject

The INJECT keyword in Ruby calls the INJECT method that is most commonly used with arrays, with the enumerable module and with converting array elements into hashes. The syntax for the include method is as follows:

inject (value_initial) { |result_memo, object| block }

You can give an initial value to the INJECT method that will be taken as the temporary value of the “result_memo” section. This is strictly optional. If you don’t specify an initial value, the “result_memo” section will automatically be assigned a value that corresponds to the first element of the collection set the include method has been paired with. The “object” section indicates an element of the collection set. The “block” section is the operation you want the computer to perform with the element(s) of your collection set. After all the operations have been performed, the result obtained will be the final permanent value of “result_memo”.

Sounds complicated? It actually isn’t! The inject method is often used to make Ruby code more readable and less complex. Let’s take an example to help you understand the concept better:

Example: Simple Arithmetic Calculations with Inject 1

Let’s assume we’ve including the enumerable mixin in our program, so its methods, including the INJECT method, are available to us. We have a simple array of numbers with us:

[5, 6, 7, 8]

What if we wanted to add the elements of the array together? We use the INJECT method for that:

[5, 6, 7, 8].inject (0) { |result_memo, object| result_memo + object }

Output: 26

How did we get the output as 26? Let’s take it one step at a time.

Step 1

First, we’ve specified the “value_initial” as 0. This becomes the first, temporary, value of “result_memo”. There are 4 “objects” in our array: 5, 6, 7, 8.The “block” section of the code will be executed once for every object in the array- which means 4 times for our example.

{ |0, 5| 0+5 }

Step 2

Now, the “result_memo” section will take a second temporary value of 5. The block will be executed again for the second time. The object used will be the second element of the array, which is 6.

{ |5, 6| 5+6}

Step 3

“result_memo” has a new value of 11. The block gets executed for the third time- this time for the element 7.

{ |11, 7| 11+7 }

Step 4

Finally, “result_memo” gets its last temporary value of 18. The block of code gets executed for the final time.

{ |18, 8| 18+8 }

And finally, “result_memo” gets assigned a permanent value of 26, which we obtain as output.

Example: Simple Arithmetic Calculations with Inject 2

What happens if you don’t pass an initial value to “result_memo”? You will get the same result:

[5, 6, 7, 8].inject  { |result_memo, object| result_memo + object }

Output: 26.

What happened here? You did not specify an initial value with “inject()”, so “result_memo” was assigned an argument that was equal to the value of the first object in the array. In this case, the code block will only be executed 3 times. Let’s take it one step at a time:

Step 1

“result_memo” is assigned a value of 5, which is the first element in our array. Because the first object has already been used, the “object” section will receive the second element of the array, which is 6.

{ |5, 6| 5+ 6| }

Step 2

11 becomes the value of “result_memo”. The third object in the array is taken up:

{ |11, 7| 11+ 7| }

Step 3

18 is the last temporary value of “result_memo”. The code block is executed for the final time:

{ |18, 8| 18+ 8| }

We obtain the output as 26. As you can see, you don’t have to specify the initial value if you want to add elements of an array together. However, if you want to perform more complex functions, like multiplication, you may need to specify a value.

Example: How to Quickly Sort through an Array and build a New One with INJECT

You can use the INJECT method to help you quickly sort through the elements of an array and choose the elements you want to build a new array.

a = [5, 6, 7, 8].inject([]) do |result_memo, element|
 result_memo  <<  element.to_s if element % 2 == 0


a # => ["6", “8”]

Here, we’re asking Ruby to build us a new array of even numbers from the set we defined earlier.

We could have used the collect method and the select method to build the array. For example:

[5, 6, 7, 8].collect { |i| I % 2 = = 0 }

Output:  [6, 8]

So why use the INJECT method at all?

The Value of Inject

There are other (apparently much easier) methods in Ruby that let you do what INJECT does, right? It’s true that the INJECT method isn’t useful everywhere- but that’s true for all methods in Ruby! The real use of the INJECT method is when you’re working on advanced Ruby programs and the code is getting too complex to read. In Ruby, you can chain several methods together and sometimes these chains get too long and too complex to understand. What INJECT does is that it breaks the chain and turns it into one large, manageable chunk of code that can be easily understood. If you’re not quite there yet, don’t worry, take a step back and revise the Ruby beginner’s program.