Making a little command line application with Ruby

Damien Cosset - Aug 29 '18 - - Dev Community

Introduction

After a first article exploring what Ruby is with the irb, I keep exploring the language. This time, I'll make a little command line tool to learn how to manipulate strings and how to prompt users for input.

Simple user prompt

Let's create a file called index.rb and ask the user for its first name:

print "First Name? "
f_name = gets.chomp
Enter fullscreen mode Exit fullscreen mode

We are simply printing a question to the terminal. By using gets, we indicate that we are waiting for a user input. So the program will wait for an answer ( it's waiting for you to press Enter actually ). Then, we store that answer to a variable called f_name.

Note: The .chomp method will remove line breaks at the end of strings. So, you can just use gets, but that will remove any unnecessary white space for later ;)

Run ruby index.rb in your terminal. You will see something like:

➜  ruby git:(master) ✗ ruby index.rb
First Name?
Enter fullscreen mode Exit fullscreen mode

And we are waiting for you to press Enter. So, you can type anything you want, press Enter, and the program will shut down.

Now, unfortunately, our little thing doesn't do anything with the variable. Let's print it back to the terminal:

print "First Name? "
f_name = gets.chomp
print "Your first name is #{f_name}."
Enter fullscreen mode Exit fullscreen mode

Notice the #{} englobing the variable name to tell Ruby that it needs to retrieve the value stored in the variable named f_name.

➜  ruby git:(master) ✗ ruby index.rb
First Name? Damien
Your first name is Damien.
Enter fullscreen mode Exit fullscreen mode

Great! We got it back!

Moar Prompts!!!

Of course, you can add more questions to your little application.

print "First Name? "
f_name = gets.chomp
print "Age? "
age = gets.chomp
print "City? "
city = gets.chomp
print "Country? "
country = gets.chomp

print "My name is #{f_name} and I am #{age} years old. I live in #{city}, #{country}."

Enter fullscreen mode Exit fullscreen mode

This time, we are also asking for the age, the city and the country of the user. We are using the same gets.chomp function from before, but store the answers in different variables.

➜  ruby git:(master) ✗ ruby index.rb
First Name? damien
Age? 26
City? paris
Country? france
My name is damien and I am 26 years old. I live in paris, france.
Enter fullscreen mode Exit fullscreen mode

Great! It works as expected. However, as you can see, I typed everything in lowercase, so everything is displayed in lowercase. Let's manipulate our answers to make it pretty

Strings manipulations

capitalize

The capitalize method changes the first letter and makes it in uppercase. In our case, there are two ways to do things:

  • create a new variable to store our change
  • change the variable's value right away
# First way
f_name = gets.chomp
f_name_two = f_name.capitalize

# Second way
f_name = gets.chomp.capitalize!
Enter fullscreen mode Exit fullscreen mode

Both will do the same thing. Just make sure that you are using the proper variable name if you are using the first way. Notice the "!" in the second way after the method name to indicate Ruby to change the current variable value and still store it in f_name. So, our program looks like this now:

print "First Name? "
f_name = gets.chomp.capitalize!
print "Age? "
age = gets.chomp
print "City? "
city = gets.chomp.capitalize!
print "Country? "
country = gets.chomp.capitalize!

print "My name is #{f_name} and I am #{age} years old. I live in #{city}, #{country}."
Enter fullscreen mode Exit fullscreen mode

And the result is:

➜  ruby git:(master) ✗ ruby index.rb
First Name? damien
Age? 26
City? paris
Country? france
My name is Damien and I am 26 years old. I live in Paris, France.
Enter fullscreen mode Exit fullscreen mode

upcase

I don't want the country's name to be displayed like this. I want it all in uppercase letters. Ruby provides the upcase method. It's used just like the capitalize method before. Replace capitalize! from country = gets.chomp.capitalize! by upcase! and the result will be:

My name is Damien and I am 26 years old. I live in Paris, FRANCE.

slice

Countries are often times displayed with the first 2 or 3 letters. The slice method will be great for this. This method takes one or two arguments. The first one is the index where you start slicing, the second is the length you want to slice. So, if I want the first two letters, I will use slice(0, 2), and that will return the first two letters. If I want three letters from the third letter, I will use slice(2, 3). Remember that we start counting from 0!

# Modify the country variable
print "Country? "
country = gets.chomp.upcase!.slice(2, 3)
Enter fullscreen mode Exit fullscreen mode

Now, the result will be:

➜  ruby git:(master) ✗ ruby index.rb
First Name? damien
Age? 26
City? paris
Country? france
My name is Damien and I am 26 years old. I live in Paris, FR.
Enter fullscreen mode Exit fullscreen mode

Conclusion

This article showed how to ask a user for a prompt using Ruby. We also saw a few methods to manipulate strings ( slice, upcase, capitalize ). Hope it was clear enough.

Have fun!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .