Most useful Ruby Enumerables when working with Object Classes — (SE Bootcamp Edition)
For those that want to do a complete 180 degree turn around in their careers and pivot into Software Engineering, learning a new skill is far from easy. It is also far from a comfortable process. When I decided to join a Software Engineering Bootcamp, my first week felt like I was trying to drink from a firehose. Luckily, I was surrounded by awesome classmates and a truly remarkable teaching staff. Once I started to understand the concept of an enumerable, for Ruby in particular, I was having a hard time to understand what tool to use when building out my methods. Thanks to my awesome coach (Shoutout Derick!), I now know what enumerable to turn to with asking just one simple question… what is the output I am looking for?
Let’s define our class and dogs first
Before demonstrating how to use our Enumerables, let’s create a class called Dog and create some Dog objects that we can enter into our array.
If the above looks confusing or scary, don’t worry. All we did here is create a class called Dog and told the computer that any time we create a new dog, we need their name, breed, and weight. When creating a new dog, that dog will be added into our @@all array. We did this so we can later work with an array that holds all of our dog objects. After declaring our class, we then created four dogs with each element entered in.
In these examples I will be working with class methods so if you are confused with the word self, below I wanted to show you what self.all looks like. You’ll notice that the first thing I do in my methods is setting this array to something more friendly, my_dogs.
Filter or Narrow down my array — (.select)
If the application we’re building is FILTERING for dogs that only weigh more than 45 lbs, the best tool to use here would be the .select enumerable. Let’s give it a try :
When we run this method on our entire array of dogs, we should only get dogs that weigh more than 45 lbs which are Fido and Marley. This is what we get back:
We just FILTERED out our array from showing all four dogs to only two that meet our condition.
Grab or Pull Out elements out of my array — (.map)
Let’s say we wanted to get a list of only the weight of each dog. Maybe we want to start running analytics on the weight of our dogs so it would be much easier if we only had a list of only the weights and not the breed or name. In this case we would use .map as this enumerable allows us to GRAB or PULL OUT data out of our array. This would looks something like:
Notice how there is NOTHING else in our array but strictly numbers that were pulled out from each dog’s weight.
Find the FIRST object in our array that meets our requirement— (.find)
In this scenario, what if we wanted our next method to return the FIRST pet that meets the requirement of having the name, “Fido”. If you remember from above, we have two dogs by the name of “Fido”. What if we only wanted to return the first one? Not both? In this situation .find will be our best tool to use.
We can use this enumerable like this:
Notice how Fido the labrador was what we got back and not Fido the corgi. That is because of the order in which we declared our dog objects. We created Fido the lab first before we created Fido the corgi therefore Fido the lab was first Fido in our array.
Anyone new to programming quickly learns that there are many different ways to solve a problem. I wanted to write this blog because I wish I had come across something like this sooner that helped me determine which enumerable to use in what scenario. Hopefully this will provide some value to anyone pursing a career in software engineering. So remember, when dealing with classes and their methods just pause and ask yourself, “What is the output I’m looking for?”.