DEV Community

Cover image for 49 Days of Ruby: Day 5 - Strings
Ben Greenberg
Ben Greenberg

Posted on

49 Days of Ruby: Day 5 - Strings

Welcome to day 5 of the 49 Days of Ruby! 🎉

Today we are going to cover the topic of Strings in Ruby. Before we begin, let's define our term. What is a String?

A straightforward definition for a String is probably:

A collection of characters.

This means that hello world! is a String, xyz is a String and abc123 is also a String.

Why are Strings important? Because, they are one of the most common data types used for when we want to store information!

Let's say you want to store a customer's favorite coffee drink in a way you can access it more easily later. I might choose to do that in a String:

favorite_coffee = 'Iced Americano'
Enter fullscreen mode Exit fullscreen mode

Now that favorite_coffee the variable contains the data I need stored as a String, I can access it later by referencing only the variable:

favorite_coffee
# 'Iced Americano'
Enter fullscreen mode Exit fullscreen mode

In yesterday's lesson we discussed briefly string interpolation as a way to combine stored strings together in a single new string. Each programming language has its own way of doing that. In Ruby we use the special character of #{} with the name of the variable inside the curly brackets.

Why is this useful? Perhaps we are building a landing page for our new coffee shop and we want to welcome our customer by referencing their favorite drink and their name dynamically.

(In this context, "dynamic" means in a way that does not involve building a separate web page for each customer, but using the same page and changing the information programmatically depending on who is visiting.)

First, we would use the variable we created above favorite_coffee to display their favorite coffee. Next, we'll create a variable to hold their preferred name: name = 'Ben', and now we'll put it all together:

"Hi, #{name}! Good to see you again. Ready to order another #{favorite_coffee}?"
Enter fullscreen mode Exit fullscreen mode

If we would run that line of Ruby code with the variables we instantiated, it would output:

Hi, Ben! Good to see you again. Ready to order another Iced Americano?

Did you notice that we used double quotes when we employed string interpolation, whereas previously we used single quotes?

We do that because double quotes send a message to Ruby that it ought to check the contents in between the quote marks for any variables it should process and render. This does not happen with single quote marks. Therefore, the exact same line of Ruby in single quote marks would output:

Hi, #{name}! Good to see you again. Ready to order another #{favorite_coffee}?

The variables inside the special #{} characters would not be processed and would just be printed as if they were normal strings.

Now it's your turn!

Open up IRB in your terminal and create some strings and assign them to some new variables. Then, play around with mixing them together using string interpolation.

Did you discover any other way you can mix strings together into a single new string? If you did, share your learning with others!

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Top comments (0)