String Concatenation & Interpolation in Ruby (With Examples)

Combining multiple strings together is something that you have to do often in Ruby.

But how can you do that?

Well…

There are two ways:

  1. Ruby string concatenation
  2. Ruby string interpolation

Concatenation looks like this:

a = "Nice to meet you"
b = ", "
c = "do you like blueberries?"

a + b + c

# "Nice to meet you, do you like blueberries?"

You can use the + operator to append a string to another.

In this case, a + b + c, creates a new string.

Btw, you don’t need to use variables to make this work.

Example:

puts "I like" + " " + "chocolate"

# I like chocolate

Another option is to use the += operator.

Example:

a = ""

a += "test"
a += "test"
a += "test"

a
# "testtesttest"

We start with an empty string & we build a bigger one by appending to it.

But there is one problem with this…

It’s very slow!

Solution coming up next.

Ruby Concat Method Example

You can use the Ruby concat method to merge strings efficiently.

Here’s how:

str = ""

str.concat("a")
str.concat("a")

str
# "aa"

It’s faster because it will change the string str, instead of creating a new one.

But this isn’t as nice as +=

Is there another way?

Yes!

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

Note: Starting with Ruby 2.4, there is a small difference, with concat you can pass multiple arguments, with << you can pass only one at a time.

Example:

str = ""

str << "a"
str << "a"

You can also do this:

str = ""

str << "a" << "a"

There is only one problem left.

If you want to combine a string with variables, but one of the variables isn't a string, you'll get an unexpected result.

Take a look:

"" + 1
# TypeError: no implicit conversion of Fixnum into String

"" << 1
# "\x01"

What's the solution?

Use the to_s method to convert all the new objects into strings.

Like this:

"" + 1.to_s

Of course, this gets ugly fast, so we have another tool you can use.

How to Use Ruby String Interpolation

Interpolation or merging of variables into strings is a powerful technique.

It allows you to "templatize" a string.

Here's an example:

age  = 33
name = "Jesus"

"Hello, my name is #{name} & I'm #{age} years old."

We can plug in any name & age by setting them as variables before the string.

The best part?

Ruby takes care of converting these values into strings for you.

So you don't have to call to_s!

Good stuff.

Prepending Strings

There is another way to combine string together.

Instead of combining variables & smaller strings into something new with "string interpolation".

Or appending to the end with << & concat.

You can also prepend!

Example:

str = ""

str.prepend("1")
str.prepend("2")
str.prepend("3")

# "321"

If you're thinking there is an append method, well there isn't.

Only for arrays.

Summary

You have learned about string concatenation, appending, prepending & interpolation in Ruby, this allows you to combine multiple strings together.

Please share this article if you found it helpful 🙂

Thanks for reading.