Ruby Input

In programming, we often need to ask the user to provide some data. Ruby uses the gets method (often in combination with the chomp method) to get input from the user.

Here's a quick example of getting user input in Ruby. You can read the rest of the tutorial to learn more.

Example

# Prompt the user for input
print "Enter your name: "

# Get user input
name = gets.chomp

# Print the input
puts "Welcome, #{name}!"

# Output: 
# Enter your name: Marilyn
# Welcome, Marilyn!

Here, we used gets to store the user input in the name variable and eliminated the newline character at the end using chomp. Then, we printed the variable.


Getting User Input Using gets

As you know, we use the gets method to get user input. Let's see what happens when we only use it without the chomp method:

# Prompt the user for input
print "Enter your name: "

# Get user input without chomp
name = gets

# Print the input
puts "Welcome, #{name}!"

Output

Enter your name: Marilyn
Welcome, Marilyn
!

Compare this program to our previous example. You'll see that the previous program printed Welcome, Marilyn! in the same line.

But this program prints ! in the next line. Why?

Because the gets method takes a newline.

You know that the ENTER / RETURN key inserts a new line in your text.

So, when the user types the input and presses this key, the new line is also added to the end of the input.

Next, we'll use the chomp method to get rid of this new line from the user input.

Note: In Ruby, a new line is represented by the newline character "\n". So, when the user enters "Marilyn", Ruby stores it as "Marilyn\n".


Getting Rid of the Newline Using chomp

The Ruby chomp method gets rid of the newline character from user input. For example,

print "Enter your name: "

# Get user input with chomp
name = gets.chomp

print "Enter your age: "

# Get another input without chomp
age = gets

# Print the inputs
puts "Welcome, #{name}!"
puts "You are #{age} years old."

Output

Enter your name: Marilyn
Enter your age: 25
Welcome, Marilyn!
You are 25
 years old.

Here, we used gets.chomp with the name variable but didn't use it with the age variable:

# Get user input with chomp
name = gets.chomp

# Get another input without chomp
age = gets

As a result, age has a newline at the end but name doesn't.

Let's rectify this problem by using chomp for both inputs:

print "Enter your name: "
name = gets.chomp

print "Enter your age: "
age = gets.chomp

puts "Welcome, #{name}!"
puts "You are #{age} years old."

Output

Enter your name: Marilyn
Enter your age: 25
Welcome, Marilyn!
You are 25 years old.

Ruby Inputs are Strings

In Ruby, the gets method reads input from the user as a string. For example,

puts "Enter two numbers: "
num1 = gets.chomp
num2 = gets.chomp

# Add num1 and num2
sum = num1 + num2

puts "Sum: #{sum}"

Output

Enter two numbers: 
1
2
Sum: 12

Here, we have two number inputs for num1 and num2. But when we added them, we got 12 instead of 3.

That's because our inputs are the strings "1" and "2". So, "adding" two strings using the + operator results in the two strings being joined together.

This is known as string concatenation:

# Concatenating (joining) two strings
puts "1" + "2"

# Output: 12

We need to convert our input to number types like integer or float to fix this issue.


Convert Input to Number

We use the to_i and to_f methods to convert the string inputs to integers and floats, respectively.

Example 1: Converting Input to Integer

Let's rewrite the previous program to convert our string inputs to integers:

puts "Enter two numbers: "

# Convert inputs to integers
num1 = gets.chomp.to_i
num2 = gets.chomp.to_i

sum = num1 + num2

puts "Sum: #{sum}"

Output

Enter two numbers: 
1
2
Sum: 3

Here, we used to_i to convert the string inputs to integers before assigning them to the variables:

# Convert inputs to integers
num1 = gets.chomp.to_i
num2 = gets.chomp.to_i

Now, we finally get the correct sum, i.e., 3.


Example 2: Converting Input to Float

print "Enter the price: "
price = gets.chomp.to_f

print "Enter the discount percentage: "
discount = gets.chomp.to_f

discount_amount = price * (discount / 100)
final_price = price - discount_amount

puts "Discounted Price: $#{final_price}"
puts "You saved $#{discount_amount}!"

Output

Enter the price: 560
Enter the discount percentage: 12
Discounted Price: $492.8
You saved $67.2!

Here, we used to_f to convert the inputs to floats:

price = gets.chomp.to_f
discount = gets.chomp.to_f
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges