• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/6/20

Combine strings together in multiple ways

Log in or subscribe for free to enjoy all this course has to offer!

Basic strings are great, but you'll often want to combine dynamic elements of text depending on certain circumstances!

Consider the following situation. When you log into a website, you often have a message welcoming you back to the site: "Welcome back, James!" or whatever your first name may be!

The website doesn't have your name hard-coded into their code base, unfortunately. ūüėȬ† The website's code is taking a hard-coded string like "Welcome back," and adding¬†your¬†name to the end of the text!¬†

Take another example from Facebook. When I log into Facebook, I see this at the top of the page:

Facebook using my first name in a string
Facebook using my first name in a string

However, if my first name were "Karin," the string in total would be "What's on your mind, Karin?"

Right now, you're learning the basic data types in theory in Ruby, but once you've got the data types nailed down, you'll start building real websites. How could you handle this common string situation of seamlessly combining two sets of text like "What's on your mind, _____?" with something like "Emily" or "Karin"?

In Ruby, this process can happen in two ways: string interpolation and string concatenation. 

String concatenation

We'll cover string concatenation first because you've already seen an example of it in this course. In a chapter in the first part of this course, you defined a Ruby method that combined a person's name with the text "says hello" for a result that looks like this: "Emma says hello!"

You did this with syntax that looks like this:

"Emma" + "says hello!"

Combining two or more strings together in Ruby can be as simple as putting a plus sign between them.

What do you think Ruby will hold as the contents of the sentencevariable?

sentence = "Welcome" + " " + "to" + " " + "this" + " " + "course."

Probably "Welcome to this course."

String interpolation

One other way to combining multiple strings together involves more of an injection approach called string interpolation. 

String interpolation is often the best option when you must access content from a database or otherwise process information in Ruby and then output that information into a string. 

For example, say that you have text prompting a user to update their profile. You want to display this to all users who haven't been active for the past 6 months. This list of users could be 50 people long, or 100, but in any case, there will be more than one person's name on the list. 

This means you won't want to hard-code someone's name directly into a string. ūüöę

Instead, you want to set their name equal to a variable and then output the contents of the variable into your primary string. That way, you can update the contents of the variable later without updating your string itself.

In order to use string interpolation, place the Ruby code you want to run inside a pound sign and a set of curly brackets #{like this}.

Here's an example of string interpolation at work:

user.name = "Tyson"
sentence = "Update your profile, #{user.name}."

 You'll notice that user.name appears inside a set of curly braces started with a pound sign:#{user.name}. We tell Ruby to runuser.name, and the result of that will appear in our nice sentence about updating a profile. 


In the interactive code exercise below, try combining some strings together for yourself using both approaches. Instructions are detailed in the exercise itself. Both string concatenation and string interpolation will become routine with a little bit of practice!


Example of certificate of achievement
Example of certificate of achievement