Enumerable#group_by

November 20, 2014

So let's say you have a collection object, such as an array, and you want to sort the array based on the result of some block of code. Turns out there's a simple way to do so using the Enumerable Method, group_by.

group_by takes a block and returns a hash. Each object in the collection is sent through the block, returning some result. Each unique result is set as the key in a new hash. The value of that key then becomes an array of the objects in the original collection which produced that key/result.

For example, let's say you have a list of names that you want to group by length.

              array = ["Lara", "Sam", "Roth", "Joslin", "Alex", "Angus", "Jonah", "James"]
              hash = array.group_by { |name| name.length }
              puts hash
              => {3=>["Sam"], 4=>["Lara", "Roth", "Alex"], 5=>["Angus", "Jonah", "James"], 6=>["Joslin"]}
            

The returned hash has keys that represent the values returned by the block (in this case, the length of each name). The values of the hash are the array elements that resulted in that particular key when passed to the block (in this case, the names).

Another example: say you want to group a list of numbers by evens and odds.

              array = [1, 2, 3, 4, 5, 6]
              hash = array.group_by { |number| number % 2 }
              puts hash
              => {1=>[1, 3, 5], 0=>[2, 4, 6]}
            

In this case, the numbers in the array are sent through the block of code. 1 returned 1, 2 returned 0, 3 returned 1, 4 returned 0, and so on. Each unique returned result (1 and 0) is a key in the hash, and the values are the numbers in the array that returned that value.

group_by is thus a handy method for sorting collection objects into hashes based on the result of a block.