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:
OutputPlease enter your name.
Sammy
Hello, Sammy! I'm Ruby!
You should have a local Ruby development environment set up on your computer. Set one up by following one of these tutorials:
To write the “Hello, World!” program, open up a command-line text editor such as nano
and create a new file:
- nano hello.rb
Once the text file opens up in the terminal window you’ll type out your program:
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.
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.
- ruby hello.rb
The program will execute and display this output:
OutputHello, 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.
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:
- nano greeting.rb
First, add this line, which prompts the user to enter their name:
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:
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:
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:
- 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:
OutputPlease 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:
- nano greeting.rb
Locate this line in your program:
name = gets
And modify it so it looks like this:
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:
- ruby greeting.rb
This time, after you enter your name and press ENTER
, you get the expected output:
OutputPlease 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.
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.
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.
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