As a new student entering the world of software development, I wanted to learn as much a possible. Sometimes, there is just too much to learn and digest all at once. At a certain point, it becomes information overload. Then there’s also that one article in the pre-course read me that vividly states all (or maybe most) software developers are lazy. I could see why people can get lazy in general, but theres got to be a better reasoning for software developers. Once I learn of gems, I realized the real explanation . It wasn’t because software developers were lazy, its because we want to solve and get to the solution in the most concise and efficient way possible.
In Ruby, one of the very first methods you will come across will be gets and gets.chomp. This method essentially lets you ask for user input (gets) and then removing the line break after (.chomp). Below is an example of gets.chomp being used in VSCode to obtain and create a username.
But since the user is creating an account, they would also need to create a password. This time we don’t want the input to be visible. How do we go about with it? Normally, we would have to look for other methods and install additional gems. If we use the method below to hide the input, we need to require ‘io/console’ and install the gem. We could keep doing that. But for each method we come across, we might need to install a gem and then require the gem. After a while, it just isn't efficient.
One of the most useful gems set I came across when building my CLI appl with my project partner was TTY. Within the TTY gem family, TTY::Prompt is one of the more popular gems with over 4 million downloads. In order to use this gem, we need to first install in our VS Code (Visual Studio Code). You first add the following to your Gemfile:
Then open up your console and run bundle install. TTY::Prompt that allows you different ways variations where you can ask for user input. For example below, prompt.ask works very similar to gets.chomp, where it allows you to get an open ended user input. While prompt.mask allows you cover up the open ended user input.
Another method you can use is prompt.select. This allows you to control the user input where they can only answer based on the choices provided.
If you want the users to choose multiple options, you can use prompt.multi_select(“ ”). Or if you want the users to be select the option by typing in numbers, you can do prompt.enum_select(“ ”). TTY was the more efficient tool compare to using the gets.chomp method.
Overall TTY has a whole array of gems that are extremely helpful when making a CLI app. Just to list a few more additional ones :
tty-cursor : moves your terminal cursor, great for board games
tty-font : enlarged terminal friendly fonts, great for welcome page
To learn more and test out all the variation of TTY gems, I have linked their page below.