As I began my journey into learning how to code to become a software engineer, I can say with all my heart of hearts that, “ Yes, you will be frustrated, and YES! You will want to throw your laptop out of a window!”. While I may be over-exaggerating a bit much, I am speaking with experience.
You are probably reading this because you are attending/ will be attending Flatiron School and you want to conquer the dreaded Hashketball lab, yet, you just can't seem to understand how to do that! Fear not, I was in your shoes about 4 weeks ago when I had no idea how to even start! In order to understand this lab, I had to ask for help from the Learn Student aids at least 5 times a day. I personally thought that was an embarrassing number of times to ask someone for help, however, to them it was extremely normal. Every question I ask was a step closer to obtaining the skills to be a better coder! So to help you on your journey to conquering this lab, I will be guiding you on the first method!
Moving forward, the best way to take on this lab was to break it into as many pieces as you possibly can! The best tool for that is and always will be “binding. pry”, it literally can“pry” open the code and takes you into a deeper look to help you debugging issues you run into. You can find out more about binding. pry here:
The first step into this lab is building it from scratch! We have a method called game_hash, which has a hash of hashes! In our game_hash there are two teams, Home & Away. In those teams we have, the team names, their colors, and their players, which includes an array of hashes of the player stats: player_name, number, shoe, points, rebounds, assists, steals, blocks, and slam_dunks. Luckily for you, the previous lab helped you understand how to create this monster of a Hash of Hashes!
Our first method will be to build a method,
shoe_size, that takes in an argument of a player's name and returns the shoe size for that player.
— Think about how you will find the shoe size of the correct player. How can you check and see if a player’s name matches the name that has been passed into the method as an argument? —
The tricky part about accessing values in a hash of hash or (HoH) is that you must get familiar with ruby enumerable and how to use them. In this case, we utilize each method!
def shoe_size(player_name)game_hash.each do |team_place, team_info|
We first used each enumerable because it has the ability to iterate over a key, value pair in our blocks that I have named team_place & team_info. Using a
binding.pry here will give you the values of
home: and the
team_name: a hash that gives us the keys and values that the
home: the key has inside, yet this is not enough for us to grab the player's shoe.
This calls for another iteration, which our handy dandy each method can do for us! Now that we have access to our hash of hashes — thank’s to our
team_info — we can use another each method to provide access to the
team_info[:players].each do |player|
Remember, we are trying to find the player name that matches the player_name our method’s parameter! Using bracket notation on our players key, we can access the player_names and find a comparison.
So with our favorite helper
binding.pry we find that our player block contains a hash of the player name we are looking for!
Now that we have that information in our hands we now can do a comparison, so if a player at the key of
:player_name and player_name is a match, then we need to return that player’s shoe size!
if player[:player_name] == player_namereturn player[:shoe]end
TA-DAA!! There you have it, we have just conquered our first method in the Hashketball lab! I know it seems confusing and frustrating, that is okay, this lab and many more are designed to have you break down and simply to the nitty-gritty! Fortunately, for us, binding.pry and ruby documentation is available to us at our disposal.
Sorts using a set of keys generated by mapping the values in enum through the given block. The result is not guaranteed…
Please use these resources to your advantage, during your journey to becoming a software engineer, documents, Stackoverflow, Dev.to. The list goes on!
I hope you find my first blog post to be helpful and enjoyable. Any and all feedback very much welcomed & appreciated!