As the coursework for Dev Bootcamp grows evermore challenging, I can feel myself gaining momentum. Each time I complete a solution I feel a sense of progress that I've been longing after for some time now. It's that little bit of "Whoa I just came up with that answer? But look at it!". I would have scoffed at my answer only a few weeks ago, had I seen it in a forum somewhere, purely because I couldn't follow the code. This progress is a beacon of light in a long dark world. Let's keep running towards it.
In this blog entry I'm going to walk you through the enumerable group_by method. If you're anything like me you might have just googled the word "enumerable". Well jokes on you because I'm about to explain exactly that.
The Enumerable Module
In the Ruby programming language there exists what are called "collection classes". These are your arrays, hashes, ranges, and sets. For the sake of this blog (and my sanity) I'm only going to talk about arrays and hashes here. So if you're not sure what those are check out this blog on them. Now if you've done any kind of work with Ruby, you'll probably have used the "each" method. Basically, it tells the computer to loop through your code performing the specified tasks a specified amount of times. This is a key component of the ENUMERABLE MODULE.
What's a module you ask? Well parked inside the metaphorical collection class garage is a pretty fly enumerable module-T. Get it? Like...a Model T. One of those old cars...
Anyways, lame jokes aside, this particular car being parked here means that this garage has full access to all the methods included in it. You could park an Enumerable Module-T in any class garage as long as that garage already has an each method minibike in there. Still with me? If not, don't worry too much. That's basically all you need to know about it for this blog.
group_by
So let's say that for whatever reason you were writing a program that needed you to take the items in an array and group them by a quality that some of them share. You know how when your mom does the laundry she seperates the clothes into whites, brights, and darks? I say "your mom" only because if your anything like I am your laundry is put in regardless of it's color. It's not that I'm really lazy... I just don't like to discriminate...
Well, when dear old mom picks up the laundry, she's processing it kind of like your computer does while using the enumerable method "group_by". Consider the following:
dirty_laundry = ["bright", "dark", "white", "dark", "dark", "bright"]
three_bins = dirty_laundry.group_by {|color| color[0] }
p three_bins
==>
{
"b"=>["bright", "bright"],
"d"=>["dark", "dark", "dark"],
"w"=>["white"]
}
In the code above, we initialized an array representing moms dirty_laundry hamper. Inside the dirty_laundry we put a bunch of strings resembling each item of clothings color. Then we used the group_by method to sort the strings by their first letter tossing each in turn from first to last into a designated bin. So basically it starts at the top of the laundry pile and see's that it's first color = "bright". Then it enters it into a new hash we've called three_bins. note: Since the group_by method outputs a hash, we don't need to initialize three_bins as a hash.
But how is it going to designate what goes in what bin? Well group_by is going to create this new hash with the result of whatever you put in the block as the KEYS. So it's first color = "bright" and in the block we said color[0] accessing the first character in the string at index 0. Thus the method creates a KEY called "b" and places the current element ("bright") into an ARRAY as the VALUE. Then comes the second element "dark" which it then establishes into a new array as the value of the key "d". It goes on to each element in the array putting each one into the value array of key representing it's first letter.
One more quick example for the road:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_hash = array.group_by { |num| num > 5 || num == 3 }
p new_hash
==>
{false=>[1, 2, 4, 5], true=>[3, 6, 7, 8, 9, 10]}
See how by going through the array num by num checking whether each num is greater than five or whether the num is 3? The first number in the array is 1, which is not greater than five, also not 3, false. So the first key in the hash is false. Then num changes to 2. Is two greater than five or equal to three? Nope, put it in the false bin. Then num changes to 3. Is that greater than five? No...but hot darn it sure as shoots equal to 3. A new key is added called true and everything that returns true for the rest of the nums is put into the value array. See how that works?
Hope this helps your learning! Thanks for reading and happy coding!
- Ricky