Interpolation-super-power
Objectives
Interpolate arbitrary code into a string
Interpolate elements of an array into a single string
Overview
We've talked about string interpolation, but let's review quickly. String interpolation allows us to evaluate Ruby code, and inject the return value into the middle of a string. Normally, we see something like this:
We're not just limited to injecting simple variables, though. We can do slightly fancier things if we just think of the pound sign/curly braces (#{}
) as delimiters that allow us to run arbitrary code in the middle of a string. For example, there is nothing preventing us from doing something like this:
Almost anything is fair game here. In fact, we're not even limited to just one interpolation per string. It's perfectly acceptable for me to do the following:
This means we can do some pretty awesome stuff! Let's say we wanted to print out a business card for our friend, Bob. We want it to look like this:
At first blush, it might make sense to do something like this:
But what if, now, we also want to print a business card for Stefani? With this pattern, we'd need to start creating a whole bunch of variables:
Three variables per person is going to get really, really messy once we want to print 5, 10, or 100 business cards. We can clean this up quite a bit by considering that name, age, and occupation really just comprise a list of attributes about a person. We have a really great data structure for representing lists in Ruby...an array!
With that in mind, let's represent our data as an array:
If we wanted to print out Bob's name, we'd do it like this:
There's an extra step there, though! As we know, we can run whatever Ruby code we want inside the #{}
in a string. So we can just access the first element in the array directly within the string:
This is going to clean up our business card printer quite a bit!
Nice! But even this is a bit much. The fact that we have to repeat so much code is a pretty good sign that we need a printer method.
So let's do that! Let's make a method, #print_business_card
, that accepts an array representing a person, and then prints out their business card. First, we'll create the basic method signature:
Now, let's re-use the code we've already written to print out the passed-in person's details:
Something is missing, though. Our people need a phone number. Let's change our people arrays to look like this:
And in our #print_business_card
method, let's print that out on a second line:
Now, let's use this! Let's print out business cards for Bob and Stefani:
String interpolation is basically a super power. And like any good super power, mastery comes with practice! (That's how Superman got so good at flying, right?) Your turn!
Instructions
Define a method,
#display_rainbow
, inlib/display_rainbow.rb
.#display_rainbow
must accept an argument, an array of colors. The tests call#display_rainbow
with the following invocation:display_rainbow(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
.#display_rainbow
should print out the colors of the rainbow in the following format:"R: red, O: orange, Y: yellow, G: green, B: blue, I: indigo, V: violet"
by reading from the array passed in as an argument. (For this lab it is OK to hardcode the uppercase letters.)It should accept an array containing the colors as an argument.
Run
learn
locally until you pass.Submit the lab.
colors
will be passed in as: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
You must read from the colors
argument and you should hardcode the order. Do not use #each
or any loop. For example, given letters = ["b","a","c"]
to print them in alphabetical order without iteration you should:
Last updated