Ruby Chunks

There are many ways to gather array elements with common values. Conceptually, one could write a method that returns a boolean based on that value. This helper could then be used in some search algorithm as we iterate through the array.

Thankfully, like most other algorithms, Ruby has this abstracted for us.

#chunk is a method belonging to the Enumerable mixin. It creates a collection of arrays, each of which consists of the common value according to the block passed and a sub-array of those elements grouped together on the basis of that value. Since #chunk is an enumerator, the method yields each array one at a time.

We can pass the #each method on the #chunk enumerator. This allows us to pass a block to each array yielded by #chunk. The block passed to #chunk simply checks for what the element in outerArray is. We then passed a block to the #each method that simply prints out the array yielded by #chunk.

Each iteration outputs an array of the following:
1. the element in initialArray; and 
2. a sub-array of the elements that are also that element.
The common value here is the element itself for the current iteration.
Only one element shares that value, so the sub-array only consists of one
element: that element which constitutes that common value.

Let’s pass a more interesting block to #chunk.

Here, we passed the #class method to each element in outerArray. Like in the previous code, we then passed a block to each yielded array that prints
it out to the console. As we can see, the common value in each array is
the class of the element for the current iteration in outerArray. The sub-array consists of those elements that share that common value, i.e. the outerArray element’s class. First is an element of the Fixnum class; thus, the accompanying sub-array consists of those elements that likewise belong to the Fixnum class.

The next array has the class Symbol as the common value. Accordingly, the
sub-array consists of elements that belong to that class, i.e. :toast and
:cream. However, notice that 4 is placed in an array of its own, even though it belongs to the Fixnum class just like 1 and 2. This reveals a limitation of the #chunk method, which is that it only groups together adjacent elements.

Let’s fiddle with the #chunk method a little more. We can pass a block that yields a boolean.

Our block for #chunk checks whether the element is a Symbol. The yielded array consists of the resulting boolean and the adjacent values that share that boolean. 1 and 2 are grouped together because neither belong to the Symbol class. :toast and :cream, on the other hand, have their own array because they do. Finally, although 4 is not a Symbol, it gets its own array because it is not adjacent to 1 and 2.

There’s one more thing I need to show before concluding this post. Again, #chunk yields the inner elements in the array that share a common value. It is also possible to access the common value directly and use it in the block passed to #each.

Suppose, for example, that we only need groups of Symbols out of outerArray.

The temporary variable boolSymbol represents the common value in the group, i.e. a boolean for whether outer_elem is a Symbol. The code will only print the sub-array if boolSymbol is true. In other words, we will only see sub-arrays of the Symbols in outerArray.

This ends our discussion on Ruby’s #chunk method. We’ll revisit this topic in the future to talk about some practical applications in a development setting. We’ll also discuss some workarounds on a limitation to #chunk, i.e. that it only groups together adjacent elements in the outerArray.