Tutorial

How To Write Your First Ruby Program

Updated on January 26, 2023
How To Write Your First Ruby Program

Introduction

The “Hello, World!” program is a classic and time-honored tradition in computer programming. It’s a small and complete first program for beginners, and it’s a good way to make sure your environment is properly configured.

This tutorial will walk you through creating this program in Ruby. However, to make the program more interesting, you’ll modify the traditional “Hello, World” program so that it asks the user for their name. You’ll then use the name in the greeting. When you’re done with the tutorial, you’ll have a program that looks like this when you run it:

Output
Please enter your name. Sammy Hello, Sammy! I'm Ruby!

Prerequisites

You should have a local Ruby development environment set up on your computer. Set one up by following one of these tutorials:

Step 1 — Writing the Basic “Hello, World!” Program

To write the “Hello, World!” program, open up a command-line text editor such as nano and create a new file:

  1. nano hello.rb

Once the text file opens up in the terminal window you’ll type out your program:

hello.rb
puts "Hello, World!"

Let’s break down the different components of the code.

puts is a Ruby method that tells the computer to print some text to the screen.

The puts method is then followed by a sequence of characters — Hello, World!, enclosed in quotation marks. Any characters that are inside of quotation marks are called a string. The puts method will print this string to the screen when the program runs.

Some methods, like the puts method, are included in Ruby by default. These built-in methods are always available when you create Ruby programs. You can also define your own methods.

Save and exit nano by typing the CONTROL and X keys, and when prompted to save the file, press y and hit ENTER.

Let’s try your program out.

Step 2 — Running a Ruby Program

With your “Hello, World!” program written, you are ready to run the program. You’ll use the ruby command, followed by the name of the file you just created.

  1. ruby hello.rb

The program will execute and display this output:

Output
Hello, World!

Let’s explore what actually happened.

Running the ruby command launched the Ruby interpreter. The Ruby interpreter read the file you specified and evaluated its contents. It executed the line puts "Hello, World!" by calling the puts function. The string value of Hello, World! was passed to the function.

In this example, the string Hello, World! is also called an argument since it is a value that is passed to a method.

The quotes that are on either side of Hello, World! were not printed to the screen because they are used to tell Ruby that they contain a string. The quotation marks delineate where the string begins and ends.

The program works, but you can make it more interactive. Let’s explore how.

Step 3 — Prompting for Input

Every time you run your program, it produces the same output. Let’s prompt the person running your program for their name. You can then use that name in the output.

Instead of modifying your existing program, create a new program called greeting.rb in the nano editor:

  1. nano greeting.rb

First, add this line, which prompts the user to enter their name:

greeting.rb
puts "Please enter your name."

Once again, you use the puts method to print some text to the screen.

Now add this line to capture the user input:

greeting.rb
puts "Please enter your name."
name = gets

This next line is a little more involved. Let’s break it down.

The gets method tells the computer to wait for input from the keyboard. This pauses the program, allowing the user to enter any text they want. The program will continue when the user presses the ENTER key on their keyboard. All of the keystrokes, including the ENTER keystroke, are then captured and converted to a string of characters.

You want to use those characters in your program’s output, so you save those characters by assigning the string to a variable called name. Ruby stores that string in your computer’s memory until the program finishes.

Finally, add this line to print the output:

greeting.rb
puts "Please enter your name."
name = gets
puts "Hi, #{name}! I'm Ruby!"

You use the puts method again, but this time you use a Ruby feature called string interpolation, which lets you take the value assigned to a variable and place it inside of a string. Instead of the word name, you’ll get the value you saved in the name variable, which should be the name of the user.

Save and exit nano by pressing CTRL+X, and press y when prompted to save the file, then hit ENTER.

Now run the program with this command:

  1. ruby greeting.rb

You’ll be prompted for your name, so enter it and press ENTER. The output might not be exactly what you expect:

Output
Please enter your name. Sammy Hi, Sammy ! I'm Ruby!

Instead of Hi, Sammy! I'm Ruby!, there’s a line break right after the name.

The program captured all of your keystrokes, including the ENTER key that you pressed to tell the program to continue. In a string, pressing the ENTER key creates a special character that creates a new line. The program’s output is doing exactly what you told it to do; it’s displaying the text you entered, including that new line. It’s just not what you wanted. But you can fix it.

Open the greeting.rb file in your editor:

  1. nano greeting.rb

Locate this line in your program:

greeting.rb
name = gets

And modify it so it looks like this:

greeting.rb
name = gets.chop

This uses Ruby’s chop method on the string that we captured with gets. The chop method removes the very last character from a string. In this case, it removes the newline character at the end of the string created when we pressed ENTER.

Save and exit nano. Press CTRL+X, then press y when prompted to save the file.

Run the program again:

  1. ruby greeting.rb

This time, after you enter your name and press ENTER, you get the expected output:

Output
Please enter your name. Sammy Hi, Sammy! I'm Ruby!

You now have a Ruby program that takes input from a user and prints it back to the screen.

Conclusion

Now that you know how to prompt for input, process the results, and display output, try to expand your program further. For example, ask for the user’s favorite color, and have the program say that its favorite color is red. You might even try to use this same technique to create a simple Mad-Lib program.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


Tutorial Series: How To Code in Ruby

Ruby is a popular object-oriented programming language. You can use Ruby to write anything from simple scripts to complex web applications. Open your favorite text editor and follow these tutorials to start exploring Ruby.

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This i svery similar to other interpreted languages lik PHP /Python

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel