Follow Up to my First CLI Application (Adding Find_by_Type Functionality)

Michael Werling
4 min readMar 26, 2021

Over the last week, I have had the opportunity to get my first working application looked over by a few students and one of our instructors at the Flatiron School. Through this experience, I had the ability to participate in some live coding exercises that not only improved my application, but also helped further my knowledge of the overall Ruby language. Today, one of my instructors thought it would be a good idea to add the functionality to search for each Pokemon type by name instead of just selecting each type by an indexed number.

This experience challenged me to have to recollect how the .find enumerable works or go to Google to get specifics on the formatting and what type of values come from this process. Specifically, I needed to add a new class method to the Type class that I had created earlier. This new method would allow my program to look through all of the original Pokemon type data from the API and FIND the one that matches the user’s typed input.

After finally remembering the correct block structure for .find, I was able to add this new method called self.find_by_type(type_name). Since this is a class method, the “self” contained in the method’s name refers to the Type class. This nomenclature and type of method will allow us to call this in other classes by writing Type.find_by_type() (this will be important in the next step.)

Please see below for the final format for this method. The “type_name” will be set to match the input from the user later. The “self.all” method was already included in the Type class on the original application and contains an array of all of the information on each type of Pokemon. The “type.info” is what I decided to call each iteration of this information. To put it in simple English, this method will search through all of the data on each Pokemon type and will spit out the information for the one that’s name matches the user’s input.

def self.find_by_type(type_name)

— — — self.all.find {|type_info| type_info.name == type_name.downcase}

end

Now that I had a find_by_type method defined in the Type class, I had to expand upon my previously written menu method from the CLI (Command Line Interface) class. This method was really the heart of the application since it allowed the user to enter the type of Pokemon they wanted to view information on. As I mentioned earlier, the application originally gave users the ability to select a type based on an indexed number, but now I could use our new method from above to add a typing functionality. Additionally, I wanted to keep the current error handling, which continued to ask for a correct type if a user entered an incorrect number (or, now, typed an incorrect word.)

The error handling was actually pretty easy to figure out. Since .find enumerables spit out nil if no values match the one being searched for, I amended my original if/else statement by asking if the Type.find_by_type method (I told you that wording would be important!) returned a nil value. If that happened, the prior written code would place the user back at the main menu asking for a correct Pokemon type. If Type.find by_type was able to return information on a Pokemon type, the program would take that information and input into the display_pokemon_details(xx) method that was previously defined in the API class. Please see below for a new copy of the menu with the additional code highlighted in bold.

def menu

— — — puts “Please select a number from above or type in selection to see…”

— — — input = gets.chomp

— — — typed_input = Type.find_by_type(input)

— — — if typed_input != nil

— — — — — — display_pokemon_details(typed_input))

— — — elsif input.to_i.between?(1, Type.all.count)

— — — — — — pokemon = Type.all[input.to_i-1]

— — — — — — display_pokemon_details(pokemon)

— — — else

— — — — — — list_type

— — — — — — menu

— — — end

end

Overall, I think the process of adding this functionality made the application more usable and made me a better coder. I wanted to thank Dominique De Leon and all of the instructors and students at the Flatiron School for making this projects the best it could be. I hope this blog post was helpful to people who are also beginning their computer programming journey and get the functionality of the enumerables confused. I will post a more informative post on a bunch of these methods next week so stay tuned!

--

--