Sitemap

The BEST Ruby CLI App Gem

3 min readJun 5, 2020

The first project that I did while attending the Flatiron Bootcamp was a command line interface application using Ruby, SQLite3, and Rails’ Active Record. The idea for the app is to learn a random fact based on numbers that you can use to start a conversation with someone. The API that I found on Rapid API was the biggest source of inspiration for this so if you’re in Mod one banging your head against the wall looking for inspiration on the web, start here! Here is a short demo of what my CLI app does:

IceBreaker CLI app

There are many useful and fun gems that are floating around the rubygems.org website but there is one in particular that made the process of making the app really enjoyable and that is the tty-prompt gem. Tty-prompt allowed for easy implementation of the scrolling selection menu and the validations for user password. Think of it as a sophisticated and cleaner way to obtain user info rather than using Ruby’s gets.chomp. The docs are super helpful as well and really outline how to use this gem in your app.

Here is a shot of the gem file in use for this application:

Press enter or click to view image in full size

Installation is just like any other Ruby gem. You can:

Add this to your Gemfile like in the above image

gem ‘tty-prompt’

And then, in the terminal, run:

bundle install 

Or, you can simply run this command in the terminal

gem install tty-prompt

in config/environment.rb file

Get Tom Tobar’s stories in your inbox

Join Medium for free to get updates from this writer.

Once installed, open up your environment file and create an instance of the Prompt module and save it to a variable. I saved mine to a constant and named it PROMPT.

Press enter or click to view image in full size

Once that is in place, you can then reference that constant where you would have used a gets.chomp and save the collected user data to a variable to do checks on it. Here, I am simply collecting a boolean from my user and then rendering the next series of questions based on that answer.

**Side note** I used puts“/n” * 35 to clear the terminal page while the app was running to simulate a ‘next page’ and it made for a tidy user experience. I sprinkled these little beauties around the app in addition to the PROMPT constant.

Press enter or click to view image in full size

Login/Validation

TTY-Prompt also made validations and the login ridiculously easy and straight forward. At the start of the self.login method, I used the .select method to give the user a choice between logging in and exiting and then used a case statement to walk the user down a path of their choosing. If my user selects the login option, they are then asked what their username is. I save that into the find_user variable and ping my DB for the collected username and check for a found user. Once found, the user is then prompted for an email and the process repeats until I have a current user set to a class variable that is available to pass around my app.

Press enter or click to view image in full size

Once again, the docs on this gem are so easy to follow that I am confident that I cannot explain them any better. They have a complete toolkit but I just used the tty-prompt. Give this a try and I hope that you enjoy working with this gem as much as I did.

Tom Tobar
Tom Tobar

Written by Tom Tobar

Motorcycle Mechanic turned Full Stack Web Dev