December 13, 2015
Entering into anything new can be overwhelming and intimidating. I should know, I have chosen to enter the field of web development and I'm attending Dev Bootcamp. Dev Bootcamp (DBC) is a development bootcamp geared at taking you from newbie to a world class beginner in 18 weeks. It is going to be one of the most challenging experiences of my life. This blog series is designed to be an introduction to the world of development for the novice developer by a novice developer. By the end of this series I hope that you have had an opportunity to grow as much as I have.
The Enumerable Module
This week we are going to be diving into the enumerable method group_by
; before we do, I guess it would be good to start at the beginning. The group_by
method is part of a special module in Ruby called the Enumerable Module. The enumerable module basically is a special set of methods that allow us to traverse, search, sort and manipulate arrays and hashes more easily. Imagine enumerable methods as the loop
method on steroids.
group_by
Although there are several methods within the Enumerable Module, as you can see in the Ruby docs, I have decided to focus on the group_by
method. All right, well from the name of the method I bet that you can figure out what this method will do, the group_by
method groups elements. But which elements and how? Good question, essentially, the method groups the elements of an array and puts them as the value into a hash and it uses the results of the code block as the key. I know that sounds confusing, so let's take a look at some code.
The above is an expanded example of what is in the Ruby docs, so let's break it down! Firstly, we are generating a range of numbers from 0 up to, but not including, 15. We then pass the number range into the code block { |i| i % 3 }
, which is basically taking each number and returning the remainder of the number divided by 3. Pretty simple and self explanatory, but this is where the magic of the group_by
method comes into play. group_by
has returned a new hash and has set the remainder as the key and all the numbers that equal that remainder as the key's value.
All right, that works great for integers, but what about strings? Well, the group_by
method works on them as well! Let's take a look:
So here, we created an array called dogs
and added the elements of a few breeds of dogs. Next we used the group_by
method on our array with the code block to group them by the length of the array elements. So new hash is returned with the number of characters in the string as the key and the matching array elements as the value. Know it is important to remember that the " " is also counted as a character when length
is called on our array elements.
Well that seems pretty straight forward. In our first example we were using the remainder of our modulus expression as the key and the matching numbers as the value. And in our second example we used the length of characters in the strings as the key and the matching strings as the value. But what else can we do? Well, what if I told you that we could actually create a random grouping? I know it sounds pretty crazy, so let's take a look at that:
WHAT?! Wow, do you see that? It really did create a random grouping. Ok, so let's see what happened here. First, we called group_by
on the same array of dogs we used previously. Then we used { rand(4) + 1 }
in our code block, which is essentially saying create a random number from 0 up to 4 and add 1. Now I know you're thinking, why add 1? Mainly because we want to eliminate 0 as a possible answer, although in this example I guess it really wouldn't matter. But in a case where we don't want 0 as the result of the randomly generated number we add 1. As you can see by the results, it really is generating a random grouping and assigning the result of the randomly generated number as the key and a random grouping as the value.
Well that is some really powerful stuff and group_by
is just one of the methods available to us in the Enumerable Method. I highly recommend spending some time reading through the Ruby docs to see everything you can do. I've also found that sometimes it helps to create your own examples to test and see if the returned value is what you thought it was going to be!