Inject, The Ruby Magic
Sep 29, 2015
I haven't used inject method much it's probably because I didn't understand fully what it can do. I came across the interview question to write a single line of code that prints the Fibonacci sequence of any length as an array using 'inject' function.
I was hella confused. What do you mean by using inject function?
So, I was reading inject method from ruby docs and found that there are much more I can do other than iterate numbers and add them all.
The argument after inject is optional but if it is passed, it will iterate through the element and 'add/dump' in there.
In the above example, this is exactly same operation as:
Now that I am more comfortable with basic inject, time to explore real beauty of the method.
Building a hash
This is how to build a hash out of array.
This isn't the most readable way but you can establish same thing with inject.
As the example shows, I start with an empty hash (the argument to inject) and I iterate through each element in the array adding the key and value one at a time to the result. The interesting thing is that the result of the block is the next yielded result, I need to add to the hash, but explicitly return the result on the following line.
Chaining method with Inject
If you want to feel smart and powerful, I strongly suggest using inject method with bunch of other methods chain them together. It is not super readable but it makes you feel awesome.
Building Fibonacci numbers with Inject function
In order to make fibonacci sequence, we need at least 3 variables so that it can predict the third element by adding first two elements. This is standard way of build the sequence
To use inject function, making a fibonacci sequence is easier than ever! All I need to do is add third element, which is the sum of first two elements.
The first range of number means how many times this program will run. There is no other meaning. Result is an array with prefixed element which is first two fibonacci sequence. Regardless of the element, all this does is to add last two elements in result array and then add to the sum.
For more information about inject, here is the great