Ruby String Concatenation

Ruby String Concatenation

Strings are an array of one or more alphanumerical characters and symbols. It is almost a guarantee you are going to use strings in your programs. They allow you to accept input from the users and display information to the users.

This tutorial will help you understand how to create strings, print strings, and string concatenation.

How to Create a Ruby String

In Ruby, we create a string by enclosing the characters in single or double-quotes. The following examples show valid Ruby strings.

'I am a string'
"I am string"

Ruby does not care which method you use to create a string as long as you are consistent. Hence if you open with a single quote, end with a single quote.

How to Print a String

There are two ways to display a string to the user. The first method is print.

print "Hello, I am a string"
Hello, I am a string

The print method does not add a new line when printing the strings. All the string values are appended to a single line. To illustrate this, create a file and add print statements:

print "First"
print "Second"
print "Third"
print "Fourth"

If you run the file:

ruby strings.rb

You will get an output similar to the one shown below:


The other option to display strings is the puts method. The puts method will append a new line after a string, as shown below.

puts "First"
puts "Second"
puts "Third"
puts "Fourth"

Run the program:

ruby strings.rb

Output is as shown below:


Variable Strings

You can store strings as a variable for later use. Create a variable name and pass the value as a string.

name = "John Doe"

Once you define and initialize the variable, you can use it by referring to its name.

puts name
=> John Doe

String Concatenation

String concatenation refers to the process of combining multiple strings into a single string entity.

Let us learn how to combine strings in Ruby.

Method 1 – Using (+) Operator

The simplest way to combine strings in Ruby is to use the concatenation operator. The operator will append the passed strings consecutively:

NOTE: In the case of integers and floats, the + operator is referred to as an additional operator.

Example 1

"John" + "Doe"
=> "JohnDoe"

The concatenation operator will append the first string to the next. You will notice the new string does not contain a space.

Example 2

If you want to add a space, you can add manually as:

"John" + " " + "Doe"
=> "John Doe"

Example 3

In most cases, you will need to concatenate strings with variables. To do this, you can pass the name of the variable as:

first_name = "John"
full_name = first_name + " Doe"
=> "John Doe"

Example 4

String concatenation creates a new string that you can save to a variable for later use.

For example:

full_name = "John" + " Doe"
puts full_name

Example 5

If you try to add a string to a number, you will get an error.

For example:

puts "Age: " + 10
`+': no implicit conversion of Integer into String (TypeError)

The error above indicates that Ruby can only concatenate a string to another string. Therefore, concatenating a string to an integer is not allowed.

To resolve this, we can use the to_s method. It converts the passed value to a string.

For example:

puts "Age: " + 10.to_s
=> Age: 10

Method 2 – #concat

Ruby has a built-in method to concatenate strings. The concat method will take two strings and return a new string.

Example 1

Consider the following example program:

first_name = "John"
last_name = "Doe"
puts first_name.concat(" ", last_name)

The concat method will take merge the passed strings.

Example 2

You can also use the << method, which is an alias of the concat method.

first_name = "John"
last_name = "Doe"
puts first_name << " " << last_name

String Interpolation

If you want to concatenate a variable into a string, you have to convert the variable to a string.

Doing this can lead to readability issues and errors.

To resolve this, we can use string interpolation to inject a variable into a string.

In Ruby, we use the # {} notation to inject a variable into a string.

Example 1

The following example will inject the age variable into the string:

name = "John Doe"
age = 10
puts "Name: #{name} \nAge: #{age}"


Name: John Doe
Age: 10

Using string interpolation, Ruby will convert the values to a string instead of manually calling the to_s method on the variable.


This guide has illustrated how to work with strings in Ruby. You have also learned various methods to combine strings and inject variables into a string without using the to_s method.

Thank you for reading, and until the next one, take care.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list