Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
String Concatenation6:39 with Jay McGavren
You can join strings together using string concatenation.
- IRB stands for "Interactive RuBy".
- You can run it by clicking in your console or terminal and typing
irbat the system prompt.
- When we run it, it'll show you a prompt where you can type Ruby expressions one at a time, hitting Enter after each.
- IRB will immediately show you the result of each expression. You don't need to call
2.3.0 :001 > 1 + 2 => 3 2.3.0 :002 > Time.now => 2017-09-02 13:31:38 -0700
- When you're done and you're ready to exit IRB, type
exitand press Enter. You'll be returned to the system prompt.
2.3.0 :003 > exit $
- IRB is a great way to try code out and see what it does, and even professional Ruby developers use it as a way to quickly test out ideas.
So now that we know how
irb works, let's use it to try out string concatenation.
$ irb 2.3.0 :001 > "a" + "b" => "ab" 2.3.0 :002 > "some words" + "more words" => "some wordsmore words" 2.3.0 :003 > "some words" + " " + "more words" => "some words more words" 2.3.0 :004 > myvar = "a string" => "a string"
- You can concatenate strings in variables
2.3.0 :005 > myvar + " abc" => "a string abc"
- Concatenation gives a new string, it doesn't change the string in the variable
2.3.0 :006 > myvar => "a string"
- To change the variable's value, use an abbreviated assignment operator, which we'll talk more about soon
myva2.3.0 :007 > myvar += " abc" => "a string abc" 2.3.0 :008 > myvar => "a string abc" myva2.3.0 :009 > myvar += " def" => "a string abc def" myvar 2.3.0 :010 > myvar => "a string abc def"
- Strings can only be concatenated together with other strings. Anything else, like a number, will result in an error.
- We'll be showing you a solution for this shortly.
2.3.0 :001 > 1 + "a string" TypeError: String can't be coerced into Fixnum from (irb):1:in `+' from (irb):1 from /Users/jay/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>' 2.3.0 :002 > "a string" + 1 TypeError: no implicit conversion of Fixnum into String from (irb):2:in `+' from (irb):2 from /Users/jay/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
Updating the widget store
- Using string concatenation to fix our
- We need to print a space following the question we ask the user
- We can do this using string concatenation
def ask(question) print question + " " gets end puts "Welcome to the widget store!" answer = ask("How many widgets are you ordering?")
- Let's print what the user entered so they can confirm it's correct.
answer = ask("How many widgets are you ordering?") puts "You entered" + answer + "widgets"
You entered11 widgets
- Oops! We need to add spaces surrounding
answer, so fix that:
puts "You entered " + answer + " widgets"
You entered 11 widgets
- You may be wondering why we didn't get an error, since strings can only be concatenated with other strings. The reason is, the value in the
answervariable is a string. The
getsmethod always returns strings. So even though the user entered a number, it's treated as a string. Eventually we'll have to convert it to an actual number, which we'll see how to do later.
- It still skips to a new line after printing
answer. That's something we'll have to fix later as well.
In our widget store program, the question that we're asking the user is running
right up against the space where they're suppose to type their answer.
In order to fix this, we're going to be to take the question that
we're asking the user, and add a space on to the end of it.
We can do this through ruby string concatenation or the joining of strings.
We'll show you how to do string concatenation in our main program
in a bit, but first let's try it out in a different way.
I want to show you a separate program that gets installed along with Ruby called irb.
irb stands for interactive Ruby and we can launch it by clicking down in our console
and typing the letters irb and pressing Enter.
When we run irb it'll show you a prompt where you can
type Ruby expressions one at a time hitting Enter after each.
Irb will immediately show you the result of each expression,
you don't need to call puts or anything.
It's a great way to try code out and see what it does, and
even professional Ruby developers use it as a way to quickly test out ideas.
So now that we know how irb works, let's use it to try out string concatenation.
I'm gonna resize the console so that it has a little more room on the screen.
You concatenate strings together using the plus operator.
So let's try typing one string, the plus operator and
a second string that we wanna join onto it.
You can see the result is the concatenated string, ab.
Let's try that again with slightly longer strings.
So we'll try a string that consists of some words + more words.
And you can see that they got joined together without any spaces between them.
That's something you need to be careful of if you're using actual English words.
You need to be sure to include spaces in the concatenated version.
So we'll concatenate three strings together.
Our first string, a string consisting of a single space and our second string.
And now, everything's spaced properly.
If you concatenate one string onto another that's stored in a variable,
it won't affect the string that's stored in the variable.
Let's try creating a variable named myvar, and we'll store a string in it.
And now, let's try concatenating another
string onto the string in myvar, myvar + abc.
And you can see that the result is concatenated string, a string abc.
But if we take a look at the contents of myvar, which in irb you can just
type myvar and it will print what myvar contains for you.
You can see that myvar is ineffective, it still contains just a string.
To concatenate the string and actually change the value that's held in
the variable, we can use an abbreviated assignment operator.
We'll talk about those operators more later, but
let's just do a quick demonstration.
So myvar, and we use the abbreviated +=
assignment operator, and we'll concatenate the same string on as we did before.
myvar += abc.
And you can see the result is a string abc.
But if we type just myvar to look at its contents,
we can see that its contents have been updated as well.
And if we did that again with a different string, if we say myvar += def, we can see
that another string has been concatenated on to the end of the first one.
And that the contents of myvar have been updated with that as well.
We now have a string, the first concatenated string abc and
the second concatenated string def.
Strings can only be concatenated together with other strings.
Anything else like a number will result in an error.
So if we were to take the number 1 and
try to concatenate a string on to the end of it, we'll get an error.
We'll also get an error if we take a string and
try to concatenate a number onto that.
We'll be showing you a solution for this situation shortly.
When you're done and you're ready to exit irb, type exit and
press Enter, you'll be returned to the system prompt.
Let's resize our console window back where it was, and
bring our widgets.rb code back up.
So now let's see if we can use string concatenation to fix our ask method.
As we mentioned, the question that we're asking the user is running right up
against the space where they're supposed to type their response.
We can fix this by concatenating a space character on to the end of the question.
Let's try running this again now.
So we'll say ruby space widgets.rb.
And we'll get asked as before, how many widgets are you ordering?
But notice that there's now a space between the question and the cursor.
Now let's try typing our response as we did before, and
you'll notice that it's spaced properly now, thanks to string concatenation.
It looks like there's another improvement we can make here.
Right now, we're just printing out whatever the user enters with no
So let's incorporate that into a more readable message.
Instead of puts answer, let's say, puts,
You entered, and concatenate that with answer,
and concatenate that with widgets.
So if they enter 8 widgets it'll say, you entered 8 widgets.
Let's try running this again.
But we noticed there is a problem.
We forgot to add spaces surrounding answer here so we wind up with
you entered running right up against the user's answer here in the output.
So let's go back into the code and add spaces surrounding the answer variable.
So You entered space, answer space widgets, and we'll enter 8 again.
There's a space here before the users answer, and we can see another space
down here on the second line, but why is there a line break in the middle of this?
You may also be wondering why we didn't get an error,
since strings can only be concatenated with other strings.
The reason is, the value in the answer variable is a string.
The gets method always returns strings, so
even though the user entered a number, it's treated as a string.
Eventually, we'll have to convert it to an actual number,
which we'll see how to do later.
We'll also see how to fix it skipping to a new line after printing the answer.
You need to sign up for Treehouse in order to download course files.Sign up