Jaime Rogstad
3 min readNov 3, 2021

--

Photo by Joshua Fuller on Unsplash

I love Ruby and I am always learning something new. I would like to share with you my newest favorite Ruby method: reduce (alias: inject).

From apidock.com: reduce

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

Let’s break this down with some examples.

Say we want to add some numbers together.

sum = 0
[5, 6, 7, 8, 9, 10].each do |num|
sum = sum + num
end
return sum

We start a sum at zero. Iterate over our array, and with each iteration of that array we add it to the sum. We then return the sum.

Let’s add some numbers together with a block:

If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element.

In this example, the array of numbers from 5 to 10 is the enum, sum is the accumulator value (memo), and n is the element.

[5, 6, 7, 8, 9, 10].reduce { |sum, n| sum + n }

Let’s add these same numbers with a symbol:

If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

[5, 6, 7, 8, 9, 10].reduce(:+)

If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo. So for the example above, 5 is our initial value.

Here is the problem I was solving when I found this method.

Find the Shortest String

Given an Array of strings, return the shortest string. If there is more than one string of that length, return the string that comes first in the list. The Array will have a minimum length of 1.

Array example: array = [‘dahlia’, ‘lupine’, ‘rose’, ‘daffodil’]

My first attempt at writing this method (the long way):

def find_shortest_string(array)
shortest = array[0]
array.each do |string|
if string.length < shortest.length
shortest = string
end
end
return shortest
end

Breakdown:

I store the first string from the array in a variable called shortest. I then iterate over the array of strings, comparing the length of the next string with the length of the current string. If the next string length is shorter, I assign it to the variable called shortest. Since I am only replacing the string if it is shorter, if a string of the same length is found, it will not be moved into the variable called shortest, and instead we will return the first string of that length found in the array.

Here’s the fun thing about the reduce method. We can write this same method in the following few lines of code.

def find_shortest_string(array)
array.reduce do |shortest, string|
string.length < shortest.length ? string : shortest
end
end

Here we are passing a block with the variable called shortest as the accumulator value (memo) and string as the element.

We start the iteration by comparing the length of the first string in the array with the length of the variable called shortest. The ternary operator tells our method to return the string with the shortest length. The method continues iterating through the entire array of strings until it reaches the end of the array and returns the string with the shortest length.

I hope that you have enjoyed these examples of how to use the reduce (or inject) method. I know I did!

--

--

Jaime Rogstad

Software Engineer with a passion for horseback riding, gardening, and cooking deliciously healthy food.