String interpolation
Overview
We'll cover when and how to use string interpolation.
Objectives
Interpolate variables into strings
When to Use String Interpolation
You're a party planner for Beyonce's 35th birthday and you're using Ruby to help you out with the arrangements. There is a variable called num_of_attendees
and since she's very popular, this variable points to the integer 547. You try and print the value of num_of_attendees
to the screen with the code below:
You expect this to print "There are 547 people coming to Beyonce's birthday party" but instead it prints "There are num_of_attendees people coming to Beyonce's birthday party." Why is this?
Well, that's because variables need to be interpolated inside a string to get their value, and not just referenced by their name, to print to the screen.
How You Interpolate Variables into Strings
To interpolate, you wrap the variable like #{this}
.
Let's try again:
This prints There are 547 people coming to Beyonce's birthday party.
. Yay!
Additional Practice
Let's drop into IRB and copy and paste the code from the following example.
Let's say you have a super hard question on your biology test asking you to identify the technical term for a group of flamingos.
Now, set the answer
variable equal to "flamboyance"
and run the following code in IRB:
This prints A group of flamingos is called a flamboyance.
to the screen.
Note that here you're declaring the variable answer
before calling puts
. You need to do it in this order, because our program is read by the computer sequentially. When your computer gets to #{answer}
, it won't know what that is if answer
isn't defined yet.
Another Way to Interpolate Variables into Strings
Some Rubyists write this another way, like this:
There's debate about the best practice but most people at Learn think the first way looks nicer and is easier for your fellow programmers to read.
Note:
Interpolation will only work on Strings wrapped in double quotes ""
. Single quotes: ''
do not support string interpolation, so running:
Will just print A group of flamingos is called a #{answer}.
If you were committed to using single quotes in such a case, it would be the right time to use the alternative method ('A group of flamingos is called a ' + answer + '.'
) which would work just fine.
Last updated