• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/6/20

Understand strings

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

Strings are a fundamental data type in Ruby. They're also the most accessible data type in the Ruby programming language because they resemble regular language the most!

Even better news: you've already seen strings in this course! 

Do you remember this line of code?

person_1.name = "Emma"

"Emma" in this case is a string.

String examples

Think about the text you normally see when you look at a website.

For example, on OpenClassrooms.com, you see some text at the top of the page about our paths, courses, and more. That text is a string!

On Twitter or Facebook, you see updates that your friends post. These would also be considered strings!

Strings are everywhere. They are simply a group of characters. Almost any time you want to show text on a website, you'll be creating and working with strings. 

Let's take a look at some more examples in order to understand what strings technically are in Ruby:

"This is a string!"
"Ahhhhhhhh 12345"
"Please sign in."
'is there anyone there??????'
"Welcome back to the site, John."
"Your last sign-in was August 23rd, 2017."

What do each of these lines have in common?

  • They're delineated by quotation marks, whether they're single or double quotes.

  • They have letters, numbers, or punctuation in them.

  • They have characters in them.

Even though strings in Ruby are straightforward, they can sometimes trip you up. As you learned above, strings in Ruby are always delineated by quotation marks. What if you have quotation marks as part of your string though? 🤔

"The woman said, "Hello!""

There are 4 quotation marks in the above sentence. Ruby interprets that your string starts before "The" and ends after the comma because that's the first quotation mark it sees. 

This string will therefore cause errors in your code.

In order to include quotation marks in your string without your code erroring out, you can do something called escaping characters.

What?! Characters that escape? Is this a James Bond movie? 

No! I wish.

Escaping characters is a way to tell Ruby, "Hey, I just want you to interpret this piece of punctuation as a normal piece of punctuation -- not a special piece of Ruby syntax." 

In order to escape characters, you'll use a backslash before the punctuation in question. The same quotation above, except with "escaped" quotation marks, looks like this:

"The woman said, \"Hello!\"."

You'll notice a backslash before the two quotation marks that are just quotation marks as part of the string itself. That way, Ruby will only recognize the first and last quotation marks as the ones that delineate strings.  You can see examples of this in the video at the beginning of this chapter.

Strings are omnipresent in Ruby (and any programming language, really), and you should get used to working with them! Check out the next chapter to see different ways to manipulate and combine strings together.

Example of certificate of achievement
Example of certificate of achievement