After a little over a month that I’ve started the Software Engineering Bootcamp at Flatiron School, it was time to develop my first coding project. I was so nervous about it because, honestly, I didn’t feel that I was ready for it yet, but I’m glad that I jumped from the cliff — or maybe, they pushed me, lol. I could understand a little bit more about the power of coding, related it to real-world life, and had some “aha” moments.
About my project — Sports CLI
In this application, the user can interact with it by choosing a city in Canada to see a list of sports available in that location. After the list shows up, the user can also choose a sport to see its description.
Hiding the API Key in Ruby
One of the first things that I had to do in my project was hiding an API Key from GitHub. Here a quick step-by-step that might help you if you also need to do it.
- Install the dotenv gem:
$ gem install dotenv
2. Create a .env file inside the main folder (I made a mistake at first, adding it inside my ‘lib’ folder and it wasn’t working properly) with a variable name (all caps) and the key value:
KEY_NAME=your-key-here
3. Add the .env inside your .gitignore file. This is how GitHub will “ignore” the .env file and won’t show up.
.env
4. I also added the “require dotenv/load” inside my “environment.rb” file, combined with the “require_all”:
require 'dotenv/load'
require_all "./lib/sports_cli"
5. To use the hidden key in your Class method, you can create a class variable:
@@key = ENV["KEY_NAME"]
Hint: Use the binding.pry to make sure the value of the @@key matches your key.
The clear( ) function
“The clear() function in Ruby is used to remove all the elements of the given array and returns the array with no elements.” GeeksforGeeks
When I learned about it, during the course, I never gave much importance to the clear function. I probably didn’t understand ‘when’ or ‘how’ would I need to use it.
In my CLI application, the user inputs a city name (and changes it whenever he/she wants to) to see the popular sports in that location.
After some tests, I noticed that sometimes the list showing up was from the previous location that the user input and not from the current location. That ‘when’ and ‘how’ I used it and for me, it was an “aha” moment.
self.all.clear
I need to clean all the elements from self before the user inputs another location, so when he/she enter a new location it doesn’t conflict with any elements inside of the array.
Binding.pry
The first time that I was presented by the require ‘pry’ I couldn’t get it and it also took me a few lessons until I started using it. I also remember my cohort leader, Candice, mentioning it in one of the live lessons and saying that we would use the “binding.pry” A LOT!
Yep… I was already using it before my project but it was so useful to debug my code, to see what was the value inside of that code, or where my coding was breaking.
Because of the complexity of the conditional statements (methods going back and forth) to the app be more user-friendly as possible, it is normal that, at some point, you will miss a piece of code and everything will break it. So, how can you check where is the error coming from? Binding.pry!
You can add it in every method to check if it’s being hit, what is the value of the objects, if the value passed is correct… until you find which part of your code is off.
I actually had to do that in this project. At first, my application was running with a bunch of cities around the world, and then at one point… bam! It was just working for some cities. I went back to my previously codes (this is why it’s important to keep pushing it in your GitHub), used the binding.pry a lot to see the possible errors and in the end, Candice also help me to make sure the error was coming from the API per si. Phew!