49 Days of Ruby: Day 5 - Strings
Tags: ruby
Published: 2021-04-01 04:51:14

---
title: 49 Days of Ruby: Day 5 - Strings
published: true
description: Join us on a 49 day learning journey through Ruby!
series: 49 Days of Ruby
tags: #ruby
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/9m73qaetuse3nm3y3it8.png
---

**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:

```ruby
favorite_coffee = 'Iced Americano'
```

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:

```ruby
favorite_coffee
# 'Iced Americano'
```

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:

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

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](https://twitter.com/hashtag/49daysofruby).**