My First Ruby CLI Project

HarronSam
4 min readJan 10, 2021

Everyone has been bored and by themselves at one point in there life with no idea what to do to occupy their time. I wanted to create a CLI that could suggest an activity to users so their time could be filled with something productive. For this project, I chose to use an API called “The Bored API” for the necessary data. While working on my first ruby gem, I naturally had crucial errors that needed to be fixed for the program to run properly. In this post, I would like to share the 2 biggest problems/challenges I had and what I did to overcome them during the course of this project.

Instance Variable Issue

After making a call to the API using RestClient to get the data, saving the returned data in a variable, and using JSON to parse it, my instantiating problem began. Using pry to investigate the problem, I noticed that the returned hash from the API call was being saved under one instance variable, @activity.

The problem probably has something to do with my call to the API or the call is being incorrectly parsed. I tried everything I could think of to solve this in a way where all of the instance variables would be filled with the correct value from the hash, but I could not. I decided to continue with what I had accomplished so far.

Since the instance variable, @activity, had the whole hash from the API, I had to research a solution.

.values

So knowing the instance variable contained a hash, I figured I should be able to only grab the values from specific keys and make the program do what I wanted.

I was able to use .values in my random_activity method to grab the value from the key ‘activity’ and effectively use just one piece of the hash, the name of the activity, in my project.

The output (below) was exactly what I wanted so I continued to use the .values method throughout the rest of my project where I needed data from the hash provided by the API.

Goodbye Message

The next issue I had was regarding the ‘goodbye’ message. Found when testing the ‘exit’ user input behavior in my CLI, I noticed that every time I would create a new instance (Type ’n’ to get a different activity) I would have to type ‘exit’ for every new instance I created.

This problem may seem minor, but it takes a lot away from user experience. Everyone knows great user experience is necessary when building any application and even though my CLI is very simple, I still wanted the experience to be smooth.

exit!

After doing some research, I found an article on RubyGuides called “How to Tell Your Ruby Program to Stop Running Early”. By the title alone, I knew I had my solution because I did need my CLI project to stop running early. The user should only have to type ‘exit’ once, no matter how many activities they wanted to see.

A program will run until it’s done processing all of the instructions in the code but in my case I did not want my project to continue processing. There was a lot of information in the article above, but in summary, the .exit! method will skip a SystemExit exception and not allow other parts of the program to ‘clean up’.

My goodbye method now closes the program immediately after the user types ‘exit’ and puts my goodbye message.

Final Thoughts

Overall I feel good about how my first Ruby gem turned out but I feel like my code could be refactored a lot. Specifically, I am not happy about my current instance variable and instantiating situation. I’m glad I was able to find a way around it and complete a fully functional project but I would greatly benefit seeing a more efficient way.

--

--