MY First Ruby Project !

Ronny Toribio Blanco
4 min readNov 17, 2020

Since the first day of joining Flatiron, I was so excited to start on a project already, so when it finally came time to it, I already had a pretty good idea of where to begin. I was planning on making an NBA historical index that will be able to retrieve every player that has ever played with their stats and every game there has ever been. Unfortunately, all the APIs that I wanted to choose from. Either I’d have to pay for them or I will be limited to a certain amount of API calls, and if that limit is reached I’d have to pay for it every time. So I searched the web for free and interesting APIs and I grabbed the first one that caught my eye, which was a superhero API that had information about common superheroes and other characters.

I started to look into the APIs documentation just so that I can have an idea of what the calls would look like. After briefly reviewing, I started working on my project. At first, I had no idea what a ruby gem file structure even looked like, it was only after reviewing some information about the “bundle” gem I discovered that I can generate the file structure by just typing “bundle gem <name of project>” in my terminal. This generated all the necessary files and folders to make a propper ruby gem. Here is when the troubles started appearing!

I started to think that I was only going to use two objects, CLI.rb, Hero.rb but then I noticed that API data itself could be seen as an object, so now I have three main objects to work with, “CLI”, “Hero”, and “API”. Immediately I knew I was going to need to “require” some files so in my .lib/hero_index.rb file I made sure to add “Open-Uri”, “Net/HTTP”, and “JSON” and also required all my objects so that they all knew about one another. Now that I had my environment all set I started on the “CLI” class. After looking at the API documentation, I noticed that it had two main ways of calling a hero, it was either by searching with a name or with an id. So I created two methods called #look_id and #look_name. Each method was going to essentially return a new instance of the “Hero” class, the difference between them was that in #look_id it was going to validate that the user’s input was a number and not a word or name. After these methods were done, I started working on the API class by calling the hero API and making it work interchangeably with the newly created methods in the “CLI” class. After these steps were done I started to work on the “Hero” class and made it so that it uses metaprogramming to properly assign the attributes. I later tested this method by giving them random inputs to see if any error would be raised”, none did appear until I noticed that I was not able to search for a hero with a space in the name for example “Iron man”. After debugging and looking at the endpoint in Google Dev Tools I noticed that heroes with spaces had “%20” between them, so I handled the error by replacing any spaces in the user input with “%20” so that the call will always send a full string without spaces. These were the most notable issue that I came across, from there I started to jump back and forth between the “CLI” object and the “Hero” object.

When working on the “CLI” class I started to create new methods that would just purely output the data in a given form and at the same time, I would also create a new method in the “Hero” class that will help support the “CLI” class. This was easily just asking the user what they would like to know about the hero and returning said information. I essentially completed all of the requirements of the project but I felt that it needed more of a user-friendly experience. This led me to search for a way to give color to the terminal! I also rewrote all the information that was returned to the user in a way that would look somewhat familiar to what the user may see online. After I felt happy about where I was with the project I took a day or two to relax and let my mind think about something else other than code. This seemed like it was a great idea because I noticed that my code is not running as efficiently as possible. After all, every time I created a new instance of a hero I would never call upon that instance again just create another instance of the same hero. This would eventually cause my program to use up unnecessary space to store all these instances. This led me to create a way to save each new instance of a hero and search for that instance whenever the user searches for that hero again. This made it so that the program will never take up as much memory as it has heroes to call upon.

After finally doing some testing here and fixing some minor issues there I could finally say that I fully completed my project! I was proud of myself because I managed to use the “yield” keyword in ruby. For a long time, I didn’t fully understand it until I used it in a way that would allow the user to navigate to the previous section or method without restarting the program. This was also a good way of learning how all methods and classes come together in unison to complete a complex task and how important it is t have a road map of what needs to be done. All I can say now is that I’m even more excited about the next project ahead!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ronny Toribio Blanco
Ronny Toribio Blanco

No responses yet

Write a response