Join Kevin Skoglund for an in-depth discussion in this video Strings, part of Ruby Essential Training.
In this movie we are going to be talking about the Ruby object type for strings. Strings are sequences of characters. The name comes from the fact that the characters are strung together. They can be a letter, a word, a sentence, a paragraph, or even several paragraphs. In every case, it's still just characters that have been strung together. Let's take a look at how we can define and work with strings. I am going to open up a new irb session and we've already seen a string earlier, right? We had "Hello". That's just a simple string.
It's between double quotes to let Ruby know that this is where the string starts and ends. We can also use single quotes the same way. 'Hello', and that works just as well. We could have greeting = "Hello" and target = "world", single quotes or double quotes. It works exact same way. Notice that Ruby does a conversion for us here and returns its results always in double quotes. It's because it's the same thing; this is just the way the Ruby represents that value when it's output to us. It doesn't matter.
It's stored as a string in both cases. We can also take strings and add them together, for example, greeting + " " + target. "Hello world." And we can simply add the strings together as if they were numbers. Now this is a common theme in Ruby where the syntax for something like addition is used in a commonsense way somewhere else. You can't actually add letters together, but it totally makes sense that the plus notation should smooch these together into new string in an additive way.
For example, we can also multiply strings, "Gabba" *5 is going to be "GabbaGabbaGabbaGabbaGabba". Now notice that there is a difference if we do 1 * 5, which is 5, and if we do the string '1'* 5, which is going to return "11111" to us. So it's not going to convert strings into integers for us. We're going to need to tell it explicitly if we want to swap. Otherwise, it's gladly going to just multiply out that string for us. Now if we have a single quoted string like 'I'm escaped.' How is Ruby supposed to know where the string starts and ends? It's going to see the first single quote and then when it sees the second one, it's going to think that's the end of the string.
So what we are going to need to do, we call escaping it, and that's putting a backslash in front of it, to let Ruby know this is a literal apostrophe inside of these quotes. It's not the end of the string, and notice it takes that away when it does its conversion to this double quoted version. The same thing is true for double quotes though. Let's say we had double quotes. "I said," we don't need to escape that anymore, "I'm escaped." We are going to need to put those backslashes in front of these two to let it know that those are literally meant to be quote marks inside of our string.
Now it's going to keep those in there, when it returns that value to us. But it's a way of letting you know these are actual quotes inside there. Of course, if we also needed to have an actual backslash in there, we would put in two backslashes. Let's say we just put at the beginning here the number 3 slash slash and you'll see that it kept that in there, but this is a literal single slash. If we actually output that value, let's do it up here with a puts, you'll see that we get a single slash back, and we get our single double quotes here, when it actually outputs it. Okay.
Now I know the 3 in front of it is nonsense. Now there is another very important difference between single quotes and double quotes, and that's the double quotes strings do some extra evaluation that allows us to use escape characters inside them. So for example, if I've puts " ", and inside I can put the escape character that is for a tab. So that's a tab and then I'll do an a and then another tab followed by b, and then I'll do a line return, which is nc, and then another line return nd.
Notice what that puts, and there we go. You see the tabs are in there and the line returns are in there. If we were to trade that for instead having single quotes, notice now what we get back is as if that was just literal text. So that the double quoted version is going to do that extra evaluation. Even better than that though is that double quoted strings allow you to drop in variables to be evaluated. puts, and then inside double quoted strings, I want to say we'll put the pound symbol or the hash symbol followed by open curly braces, greeting, which is the variable we defined earlier, we'll close our curly braces.
I'll put a space, and we'll do the same thing again for target, and now it will drop in those values. Let me put a period at the end. I want to say Hello world. So you see what it did. It evaluated that inside those double quotes. So now if we were to make those instead single quotes, you'll see that it took it literally. It did not do that extra evaluation. Now we can put any Ruby expression inside those braces. So, for example, puts, 1 plus 1 is equal to, and then inside here, let's ask it to tell us what is 1 plus 1? Here we go.
1 plus 1 equals 2. So it does the Ruby evaluation inside those braces and then puts the value inside the string and that only works with double quoted strings. Let's take a look at some of the methods we can use with strings. We saw one earlier, we had hello.reverse, and we also have capitalize, we also have downcase, downcase, of course we have upcase, and length, and you'll see it returns 5 to us.
That's how long the string is, how many characters are in it. I think all those are pretty commonsense. The one thing I do want to show you though is that if we take Hello, and let's say we take the reverse method, there we are, Hello reverse, and then after that we ask it to upcase, you'll see that it does both. So we can daisy chain these methods together. So reverse.upcase.length, for example. Of course, the length obviously is still 5. It hasn't changed, but the point is that every time we start with our object, we apply the reverse method to the object, and then we return an object.
Hello, backwards, and then that returned object gets the upcase method applied to it, and then the returned object from that gets the length method applied to it. So each time we're returning an object and applying a new method, and that's why it's so great that everything in Ruby is an object is because we can do this kind of thing to it. And of course, daisy chaining these methods together applies to all objects in Ruby, not just strings.
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish