Using Ruby Enumerable on Makeup?
If you’re anything like me, then I’m sure you’ve been scouring the internet for examples of .each, .select, .map, .find, and .find_all. Or maybe you’re not sure how to write out the actual method? Most of the resources available use abbreviations to represent arrays, elements and hashes, making it all the more confusing. So I figured, I should create a guide and examples using plain english… MAKEUP!
In my makeup collection, I have primers, foundations, concealers, etc. They are all from different brands, I use them in different seasons, and of course have different ratings. Let’s say I put all of my products in an array and called it, “makeup”. Each product is stored in a hash, that includes the brand name, type of product, rating, and if it’s long wear…you know for those long fun nights with your girlfriends.
But let’s be real, my list of products is way more extensive than this! Let’s say, I was packing for a trip and didn’t have time to sort through all of my makeup and I only wanted to know what brands I had, or I only wanted my best rated products, or I only wanted to pack all of my long wear makeup…you get the point. Enter Ruby enumerable!
Before we start, I want to point out that you are going to see the phrase makeup_instance inside of pipes. Please don’t read into it! That phrase is just a name holder, I could have easily used the phrase , |I_love_Sephora| and it would have done the same job as long as I used it correctly in the rest of the code.
- Using .each to get an array of all the brands:
Your output is: [“Milk Makeup”, “Tatcha”, “Smashbox”, “Benefit”, “Too Faced”, “Urban Decay”]
- Using .map to get an array of hashes of all the brands AND their product type:
Your output is: [{“Milk Makeup” => “primer”}, {“Tatcha” => “primer”}, {“Smashbox” => “foundation”}, {“Benefit” => “concealer”}, {“Too Faced” => “concealer”},{ “Urban Decay” => “setting spray”}]
- Using .select to get an array of hashes of all the primers:
Your output is: [{:brand => “Milk Makeup”, :type => “primer”, :rating => 5, :long_wear => true}, {:brand => “Tatcha”, :type => “primer”, :rating => 4, :long_wear => false}]
- Using .select to get an array of hashes of the products with a rating greater than 2:
Your output is: [{:brand => “Milk Makeup”, :type => “primer”, :rating => 5, :long_wear => true}, :brand => “Tatcha”, :type => “primer”, :rating => 4, :long_wear => false}, {:brand => “Smashbox”, :type => “foundation”, :rating => 3, :long_wear => true}, {:brand => “Too Faced”, :type => “concealer”, :rating => 5, :long_wear => true}, {:brand => “Urban Decay”, :type => “setting spray”, :rating => 5, :long_wear => true}]
- Using .find to get a hash of the first instance that matches what I’m looking for. In this case, I’m looking for long wear makeup.
Your output is: {:brand => “Milk Makeup”, :type => “primer”, :rating => 5, :long_wear => true}
- Using. find_all to get an array of hashes of all the products that are long wear:
Your output is: [{:brand => “Milk Makeup”, :type => “primer”, :rating => 5, :long_wear => true}, {:brand => “Smashbox”, :type => “foundation”, :rating => 3, :long_wear => true}, {:brand => “Too Faced”, :type => “concealer”, :rating => 5, :long_wear => true}, {:brand => “Urban Decay”, :type => “setting spray”, :rating => 5, :long_wear => true}]
Bonus:
What if you need to apply the following methods to an array of hashes within another method? Like this:
The methods above will still work! All you need to do is call on the name of the method (which in this is also called makeup) and voila! You will get the same outputs!