You Will Always Remember Your First…

Che Goranov
6 min readApr 8, 2021

by Che Goranov

Batman programming

They say you will always remember your first … CLI program you coded by yourself for your Phase 1 project at Flatiron School. Why…what did you think I was going to say? Today, April 7th, I have finally finished my first CLI application and the weird emotion after is hard to explain. I am beyond proud of this little application that most Software Engineers could do in their sleep, but hey… it is mine and I am so proud of it. So lets dive in.

In today’s world you cannot turn on a podcast, read an article or hear water cooler talk without hearing the sentence “Have you seen Bitcoin recently? Should I invest?” As a finance professional I get this question all the time. So for my first project what better first idea than a Crypto data base for all the largest virtual coins. I present to you…*drum roll*… CRYPTO SEARCHHHH.

A beauty, is she not? But what does she do? Well, it aggregates the top 5 largest virtual coins by market capitalization and lists them for you. From there, the user can select the name of the crypto coin they would like to see more detail and boom, you can get the below detailed breakout.

List of top 5 crypto currencies

So how does it all work? Well there is not enough time to go through everything but let me take you through some portions of the code I am personally most proud of. Let us start with the screenshots above of the crypto coin top 5 list. Looks cool but that is not how the data is! See the code behind the scenes below:

Code behind the scenes.

Woah! That is a lot to unpack. Do not worry, I will take you through it. In fact, lets go line by line. You will notice that I have a sorted_lists local variable that is storing the array of all CryptoCurrency Objects I have created from pulling data from my API and creating each object through my CryptoCurrency class. But, I cannot assume the API data to be in proper order of size, or in this case, ranking. As a Rubyist, we have to make this code flexible. So, on the right side of the sorted _lists variable I am calling the class method all on my CryptoCurrency class to return the array of all my CryptoCurrency Objects. From there I am using the sort method on my array to iterate through two class Objects at a time, represented by |a, b|, and sorted them by the Object’s Attribute rank using the famous spaceship operator “< = >”. Boom! So this now assures us that the list of Crypto Currency Objects are in the right order of rank 1– 100.

But the job is not done, the responsibility of this application is to aggregate the top 5. How do we do this? Well you know the new array of Crypto Objects we just stored in the sorted_list local variable? Well we are going to iterate over each one using the .each method, |x| representing each object in the array and only printing the the string with x.rank (object.attribute) and x.name interpolated if (boolean operator) the rank is less than or equal to 5! And because the array is sorted in order of rank 1 and down, this will print the Crypto Objects in order! So cool.

So what is next? Well the user is going to pick one of the top five currencies and it will display a really cool detail breakout. Let us be a cliche and use one of the big daddies, Ethereum. See the detail the program will provide below!

Absolutely beautiful but what is really cool is that the detail follows a common Finance convention. Do you see it? The % change is red when negative, and green with a “+” when positive. To achieve this in code is really quite cool. Let us take a look at the code and then dive in!

Wowza

Looks like a lot but it is not that bad! Again, lets start from the top! The currency_detail instance method starts by taking in the argument of a user_input which will be a String of the name of the crypto currency the user has selected from the top five list. The code will then iterate through the CryptoCurrency.all (the array of all created Crypto Objects) using the .each method. As the .each method iterates on the array, it will assign each object to the variable |x|. Next is the really important line. The If statement! Here we are saying if the Object being iterated on, x, name attribute equals the user_input, then we want to display the below information using String Interpolation with the syntax #{ }. SIDE NOTE: Did you notice something interesting? In the If statement, we have x.name.downcase! Why is that? Well, to prevent the program from breaking if the user does not input the Crypto Object name with the exact capitalization, our program is hedging that risk by applying the .downcase method to the user_input instance method (that is how the user actually interacts and inputs their request to the CLI), somewhere else in our program code, that makes the entire string lowercase. Then, when comparing the user_input to the x.name.downcase (in the currency_detail method above), that will make sure the Cypto Objects name attribute is also all lowercase as well and the boolean statement will trigger correctly with the Object we want! This way we can always compare “apples to apples” so to speak throughout the program.

What about those cool colors and how they adjust to the change in currency price? That is why you really read this far ;). Well my Cohort Lead and all-around MVP, Candice Peters, suggested a really cool Gem called colorize. Gems are a library of pre-written code from some awesome Rubyists out there that we can use or implement into our code. Saves us so much work. First step was to add colorize to my Gemfile:

Shiny Gems.

The naming convention here is important. Our syntax begins with gem ‘colorize’ (stating the name of the gem) and the path so the application knows where to download it from. Next, in the terminal type, ‘bundle install’ and we are in business! To format the color changes to my currency movements I coded the below method:

The glow up.

I named it the format_change method. The argument is the |x| variable and the percent_change attribute from the currency_detail method above! So what is the x.percent_change returning? A String of the percent change! We then implement the .include? method to see if the string contains a “-”. This means it was a drop in the currency price, therefore should be red. If the string contains a “-” the .include? method will return a boolean value : true or false. Now implementing control flow with our if-else statement, if the string contains a “-”, colorize the string :red (thanks to our colorize gem!) or else colorize :green with a plus sign (also colorized) to indicate it is increasing (making the big bucks!).

There you have it! Now at the next water cooler you can say the exact percentage that Bitcoin went up and how much closer you are to that early retirement. And all jokes aside, this has been an incredible experience so far. Flatiron and my cohort lead have been incredible. I am very proud of being part of this school and the project I was able to produce. Looking forward to many more.

--

--