Ruby Gets: Print User Input From the Console Using the Gets Method

ruby getsThere are plenty of ways to print output to the console with Ruby – the puts and print methods, for instance – but how does one print user input read from the console? One method in particular is the gets method. The Ruby gets method reads input provided by the user, and can reprint their input elsewhere in the program. Learn how to use the Ruby gets method with this guide.

For more help with Ruby, you can check out this Ruby on Rails beginner’s course, or refer to this step-by-step Ruby on Rails guide. Ruby on Rails is a web application framework, sometimes referred to as Rails, that lowers the barrier for Ruby programming, making it an ideal place to start for entry-level programmers.

How to Use the Ruby Gets Method

The prints method in Ruby prints text to the console. For instance:

#!/usr/bin/ruby

print "Hello"

The above code will simply print the word  “Hello” in the console. But what if we want Ruby to read user input in the console and display that as well? Or what if we want the user to assign a value to a variable? For this, we must use the Ruby gets method.

For  instance:

#!/usr/bin/ruby

print "Please enter your name: "
myName = gets
puts "Welcome #{myName"}"

The above code will print the phrase “Please enter your name,” followed by a colon and a space. Then, you create the variable myName and assign the value using the gets method. This means any input that the user enters into the console will be bound to the myName variable. Since the text printed before asked for the user’s name, then the variable name will be called myName. Next, the puts method welcomes the user based on the name they input following the firsts command.

Get up and running with Ruby on Rails in this beginner’s course.

Ruby Gets, Prints, and Puts Syntax

The difference between print and puts is important with regards to our gets example. The print method will print text on the same line unless a new line is specified with the  \n character. For instance:

print "Hello"
print "Hello"

The above code will print:

Hello Hello

If we want this to be on two separate lines, we’d write:

print "Hello "
print "Hello\n"

The above code will print:

Hello
Hello

What’s useful about the puts method is that it prints strings separately on their own line. The reason we used the print method in our gets method example above is because we want the user’s answer to be input on the same line.

Instead of:

Please enter your name: 
Catherine

We want:

Please enter your name: Catherine

Now, if we wanted it to look like the first example, with the user input on a new line, we could do one of two things. First:

print "Please enter your name below.\n"

Or:

puts "Please enter your name below."

It’s a simple detail, but it could make loads of difference in your application, especially when you’re first learning how to program. For more Ruby help, check out this Ruby on Rails for beginners course.

Ruby Gets Method Example

Either way, our example would be followed with the gets method being assigned to a variable of our choice, as long as we remember how to display that variable again in the console’s output.

print "Please enter your age below.\n"
myAge = gets
puts "You are #{myAge} years old!"

The #{myAge} tag will insert the value of that variable, as decided by user input, wherever you specify. For instance, we can create a program that generates a story based on user input. There’s no limit to the amount of gets methods we can use to pull input from the user and assign it to variables in our application.

puts "Welcome! This program will generate a short story based on your own input. That's right. Your OWN input! Let's begin. First, what's your name?"
userName = gets

puts "Hello #{userName}! How old are you?"
userAge = gets

puts "So you're #{userAge}? Wow! What is the month and day of your birthday?"
userBD = gets

puts "What is your favorite food?"
userFood = gets

puts "Where is your favorite vacation spot?"
userVac = gets

puts "What is your best friend's name?"
userFri = gets

puts "Thanks a lot, #{userName}! Here's an awesome story all about you... Once upon a time, #{userName} woke up and saw that it was #{userBD}! You know what that means... It's #{userName}'s birthday! #{userName} called up #{userFri}, made enough #{userFood} for two, and they both met up at #{userVac} for an awesome birthday party.

The above MadLib-type example will generate a short, interactive story for users to play.

With user input and the computer’s output, running  the program would look something like this:

Welcome! This program will generate a short story based on your own input. That's right. Your OWN input! Let's begin. First, what's your name?
CATHERINE

Hello CATHERINE! How old are you?
20

So you're 20? Wow! What is the month and day of your birthday?
MAY 10TH

What is your favorite food?
DIM SUM

Where is your favorite vacation spot?
THE BEACH

What is your best friend's name
VINCENT

Thanks a lot, CATHERINE! Here's an awesome story all about you... Once upon a time, CATHERINE woke up and saw that it was MAY 10TH! You know what that means... It's CATHERINE's birthday! CATHERINE called up VINCENT, made enough DIM SUM for two, and they both met up at THE BEACH for an awesome birthday party.

You can make your own MadLib-type story by using the Ruby gets method to retrieve user input and assign it as a variable value. Just remember the syntax!

variableName = gets
print "This is how you reference #{variableName} in a string!"

This is vital knowledge for entry-level Ruby programmers. To learn more, check out this Ruby programming for beginners course.

If you’re ready to move on to the tough stuff, check out this advanced Ruby programming course. If you’re still learning the ropes, here’s a beginner-level Ruby tutorial course, meant for students with zero programming experience. Learn the vital Ruby on Rails framework with this entry-level Ruby on Rails tutorial for starters.