Leanna McDonald

Web Developer

Building a Ruby CLI App

Jan 30, 2017


Running an application from the command line interface (cli) is often preferable. It is easy to include options after a command for a straightforward use of an application. This is a tutorial for creating a CLI App written in Ruby. The example we will use is an App for creating, storing and changing a list.

Overview

We will create an app to store a list of my favorite fruits. There are a number of pieces that must come together to create this app.

  1. Cover the basics of running any ruby program from the command line.
  2. Simplifying calling the program.
  3. Reading from and Writing to a file.
  4. Putting it all together.

The Basics

First we must create a basic ruby program and be able to execute it from the terminal . You can make a new text file and name it anything with a .rb extension such as fruits.rb. By using your favorite text editor, you can write your ruby code such as puts "I love fruit!". To execute the file with ruby you can type ruby fruits.rb into the command line. And voil&aacute!

Making our file executable without the extension

To make things simpler to not have to type ruby and .rb into the command line, we can set the ruby script as executable with the following steps:

  1. Include on the top of the ruby text file #!/usr/bin/env ruby. This states that ruby will be used to execute the program. Therefore, to execute the CLI App, someone no longer needs to type ruby before the file name. Additionally, the file can be renamed to not include the .rb extension. Now, only the file name must be typed and a .rb extension is not necessary.
  2. To make the ruby file executable, the permissions must be changed. By typing chmod 755 fruits we are changing the permisions to allow anyone to execute the file.

Now we can execute the ruby program by simply typing fruits

Connecting that program to a universal CLI command

So far, we have assumed that you will be running the app from the same location it is saved. Therefore, the path to the file was simply the name of the file. However, sometimes it is convenient to be able to execute a ruby app from any location from the command line in the terminal. In order to do this, we must add the file location to /usr/local/bin. We do this by typing ln -s $PWD/hello /usr/local/bin/ into the command line.

When executing a command from the terminal, the system looks in a number of locations to find the file. By linking the current location of your ruby file with /usr/local/bin/, the command will always be found despite location.

Reading from and writing to files in Ruby

Ruby is able to read and write other files. With ruby you must open the file in the appropriate mode and then it is relatively straightforward to read or write to the file.

Reading a file in Ruby

Here is an example of reading a file in Ruby:

open("fruits_list.txt", 'r') do |file|
	file.each_line do |line|
  		#here do what you want with the line
	end
end

Reading a file in ruby can be done many ways. Here I show a straightforward example of being able to read a file line by line. We use the open command and specify the name of the file. In this case the file would be located in the same place as the ruby file. Alternatively, you may need to specify the full path to the file being read. We choose the option r to read the file. Then do |file| indicates that we will have some code that follows utilizing the opened file. Next, we have another line to indicate that we would like to read the file line by line. We are able to grab each line from the file one at a time. Within this loop, we can manipulate each line of the file in any way that we wish.

Writing a file in Ruby

Here is an example of writing to a file in Ruby:

open('fruits_list.txt', 'w') do |file|
	file.puts "My favorite fruit"
end

Similar to reading a file, we use the command open to open the file, specifying the name of the file to be written to. To write to the file we choose the option w for write. The following code between do and end are included in the file with a couple choices for syntax. Here I am using file.puts to write into the file. It is also common to replace file.puts with file <<.

Putting it all together

So far we have created a nearly empty ruby file fruits that we are able to execute from the command line and we know how to read and write an external text file. We will put it togethe rto create an app that allows us to store our current favorite fruits.

Lets start with a ruby function that prints our list of fruits to the terminal.

On line 22, we run the function readFruits which reads our list of favorite fruits from the text file and places the list into an array. Then we run the function printToTerminal which goes through each item in the array of fruits and prints the name to the terminal with a number in front of it.

Now, when we run fruits from the command lin we will see a list of the favorite fruits we have saved.

Next, we would like the ability to add additional favorite fruits to our list. So, in our ruby file we would like to accept the input for fruits add grapes where this would add grapes to our list of favorite fruits.

We use ARGV to accept additional inputs after our file name. The first is add which points to running the addFruit function. The second input is the name of the fruit to be added. After adding the fruit to our array we must then write out the new file in order to save our change.

Lastly, we would like to remove a fruit if we decide we don’t like it anymore. From looking at our list of fruits

  1. strawberry
  2. grapefruit
  3. grapes We would like to remove grapefruit so we can type fruits remove 2 in order to remove grapefruit from our favorite list.

Now we are able to both add and remove fruit from our list of favorite fruits, all from the command line.

If you liked this article and think others should read it, please