Throughout the first few weeks of my Software Engineering bootcamp at Flatiron School, I’ve picked up a few idioms that present themes in this new world I’m exploring. One that has stood out to me is that “developers are lazy.” While the connotation of this phrase is inherently negative sounding, the meaning behind it is not, and holds a lot of real world application.
The concept here is that good code is not repetitive and developers will use the tools they have available to simplify an application. This mirrors a lot of common social interaction. People do not like to repeat themselves and oftentimes being concise and to the point will yield greater results.
Internal to a file, the strategies to cut down on effort are often unique. If you’re going to need to use an output, save it in a method or variable. If you’re going to leverage similar equations, use automation. However, beyond syntax and organization, Ruby provides external tools to leverage and simplify getting results: Gems.
Ruby application’s functionality is greatly enhanced through the use of gems. RubyGems was released to the public in 2004 by a number of individuals — Chad Fowler, Jim Weirich, David Alan Black, Paul Brannan and Richard Kilmer. To explore this incredibly useful topic, I was exposed to a gem called “TTY::Prompt,” an appropriate introduction as I began my first project — a CLI Karaoke Application.
It’s extremely important to read up about any gem you plan on utilizing. Every gem consists of code, documentation and a Gemspec (Gem specification). For TTY-Prompt, you can find all of the details at this link.
A few commonalities regarding gem documentation:
- Lib Directory: contains the code for the gem
- Test (or in this case spec): used for testing
- Rakefile: automates test and generates code
- README: includes the documentation
The key functionality of this gem is communication with a user. In a CLI, communication is sent out to the user through ASCII Output while users deliver input using ASCII characters entered into a prompt. In a simple back and forth, an application can “puts” out a question leaving the user to type in a response.
Conceptually, it is important that the program is clear and concise to minimize any risk of a misunderstanding from the user. Next, the program needs to actually be able to take in an input and function properly. To do so, the program uses the line:
However, often a question is not this simple. “TTY::Prompt” empowers an application with a number of prompt types for gathering user input. Throughout this post, I’ll outline a few useful use-cases.
First, understanding the setup steps is necessary for utilizing gems. If you cannot get a gem properly loaded into your file, it will not be accessible. For TTY::Prompt, the following steps are outlined:
Add this line to your application’s Gemfile:
And then execute:
These two steps are commonplace regarding gems. The Gemfile in your project will hold all gems that need to be loaded and once you have them all included, ‘bundle’ will actually load the functionality into the project.
In order to actually start asking questions on the command line, you will also need to include the following two lines:
In a file, the require line is often packaged up by including “require ‘bundler’” in your environment file. The second line here is saving the action as a variable, to simplify your questions in the command line.
Following the theme of clear questioning, the first example is a fairly straightforward one — what if you need your user to select an option from a few choices? In any application, this opportunity presented itself at the beginning of CLI Karaoke — Login, Register, or Exit. You would never want a user to have to guess what these options are, so TTY::Prompt gives you the following functionality:
Since the user choice is going to influence the remainder of the application’s flow, saving it as a variable is useful. In the syntax, you can see the question “What would you like to do?” is included first and the available responses are saved in an array of strings using “%w.”
In a similar situation, if you have many options, you are able to organize your application by leveraging a menu.
This can be cleaner than creating multiple conditionals in the first example, as you can point each menu option to a new method.
What is more simple than a yes or no question? TTY::Prompt allows this functionality through their “.yes” method. In the CLI Karaoke app, if a new user tried to login using an unregistered username, we wanted to give them an option to quickly create an account using their input.
The “.yes?” prompt automatically loads a “Y/n” response into the command line, so you do not need to type that out. It is also important to note that the return value is a boolean true or false, so you can easily set up any conditional from that logic. Here, if the user selects “Y” for creating a new account, the application creates the user. Otherwise, they are sent back to the start of the login method.
One final use-case is the concept of a dynamic list. In the application, users are able to add songs to a “Favorites” list. When they view their favorites list, the application needs to allow them to select a song from that list and interact with it like any other. The question here was — how will we be able to present this list that is constantly changing?
“TTY::Prompt” allows you to present a user with a dynamic list! In the example above, favorite_songs was the variable where we saved the array of songs. We shoveled “Exit” at the bottom of this list so they could return to the main menu.
This is just a small snippet into the functionality of this gem. The options are expansive and cover many use cases. To explore more gems and their functionality, you can go to this website.