CodeNewbie Community

Cover image for 49 Days of Ruby: Day 29 - Command Line Interfaces (CLI)
Ben Greenberg
Ben Greenberg

Posted on • Originally published at dev.to on

49 Days of Ruby: Day 29 - Command Line Interfaces (CLI)

Welcome to day 29 of the 49 Days of Ruby! 🎉

We are ready to discuss starting to build your own applications in Ruby!

One of the first application types you may want to build is a CLI, or a command-line interface.

A CLI is a form of application that runs as text, often in a user's terminal. You have interacted with lots of CLIs already, from when you started by installing Ruby to yesterday when you learned a bit about debugging!

Today we are going to discuss the file contents of a CLI, and tomorrow, we will discuss how to accept user input.

A CLI in Ruby needs the following at the top of it:

#!/usr/bin/env ruby

Enter fullscreen mode Exit fullscreen mode

That #! followed by the /usr/bin/env ruby tells your computer how to run the rest of the code. In our case, we want it to be run using ruby installed on the system.

Go ahead and create a file in your terminal called cli.rb and add that line at the top.

Then add on a new line:

puts "This is my CLI!"

Enter fullscreen mode Exit fullscreen mode

Once you save the file, from your terminal type ./cli.rb.

Did it not work? We need to designate the file as an executable first! Run the following: chmod +x cli.rb. This changes the file to be an eXecutable type.

Now, try the ./cli.rb again.

You should see "This is my CLI" in your terminal. Congrats you built and ran your first CLI!

Tomorrow, we will make our CLI be more dynamic, and a bit more practical, by learning how to accept user input.

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Discussion (0)