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 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.

It still skips to a new line after printing answer. That's something we'll have to fix later as well.

0:00

In our widget store program, the question
that we're asking the user is running

0:04

right up against the space where
they're suppose to type their answer.

0:07

In order to fix this, we're going
to be to take the question that

0:10

we're asking the user, and
add a space on to the end of it.

0:13

We can do this through ruby string
concatenation or the joining of strings.

0:18

We'll show you how to do string
concatenation in our main program

0:22

in a bit, but
first let's try it out in a different way.

0:25

I want to show you a separate program that
gets installed along with Ruby called irb.

0:31

irb stands for interactive Ruby and we can
launch it by clicking down in our console

0:36

and typing the letters irb and
pressing Enter.

0:39

When we run irb it'll show
you a prompt where you can

0:42

type Ruby expressions one at
a time hitting Enter after each.

0:50

Irb will immediately show you
the result of each expression,

0:53

you don't need to call puts or anything.

0:55

It's a great way to try code out and
see what it does, and

0:58

even professional Ruby developers use
it as a way to quickly test out ideas.

1:02

So now that we know how irb works, let's
use it to try out string concatenation.

1:07

I'm gonna resize the console so that it
has a little more room on the screen.

1:13

You concatenate strings together
using the plus operator.

1:16

So let's try typing one string,
the plus operator and

1:20

a second string that
we wanna join onto it.

1:22

You can see the result is
the concatenated string, ab.

1:26

Let's try that again with
slightly longer strings.

1:29

So we'll try a string that consists
of some words + more words.

1:38

And you can see that they got joined
together without any spaces between them.

1:41

That's something you need to be careful
of if you're using actual English words.

1:47

You need to be sure to include
spaces in the concatenated version.

1:53

So we'll concatenate
three strings together.

1:56

Our first string, a string consisting of
a single space and our second string.

2:03

And now, everything's spaced properly.

2:05

If you concatenate one string onto
another that's stored in a variable,

2:09

it won't affect the string
that's stored in the variable.

2:12

Let's try creating a variable named myvar,
and we'll store a string in it.

2:20

And now, let's try concatenating another

2:25

string onto the string in myvar,
myvar + abc.

2:30

And you can see that the result is
concatenated string, a string abc.

2:35

But if we take a look at the contents
of myvar, which in irb you can just

2:40

type myvar and
it will print what myvar contains for you.

2:46

You can see that myvar is ineffective,
it still contains just a string.

2:51

To concatenate the string and
actually change the value that's held in

2:54

the variable, we can use
an abbreviated assignment operator.

2:58

We'll talk about those
operators more later, but

3:01

let's just do a quick demonstration.

3:03

So myvar, and we use the abbreviated +=

3:07

assignment operator, and we'll concatenate
the same string on as we did before.

3:12

myvar += abc.

3:14

And you can see the result
is a string abc.

3:17

But if we type just myvar
to look at its contents,

3:21

we can see that its contents
have been updated as well.

3:23

And if we did that again with a different
string, if we say myvar += def, we can see

3:30

that another string has been concatenated
on to the end of the first one.

3:34

And that the contents of myvar have
been updated with that as well.

3:39

We now have a string,
the first concatenated string abc and

3:43

the second concatenated string def.

3:46

Strings can only be concatenated
together with other strings.

3:50

Anything else like a number
will result in an error.

3:53

So if we were to take the number 1 and

3:56

try to concatenate a string on to
the end of it, we'll get an error.

4:02

We'll also get an error
if we take a string and

4:06

try to concatenate a number onto that.

4:09

We'll be showing you a solution for
this situation shortly.

4:12

When you're done and
you're ready to exit irb, type exit and

4:15

press Enter,
you'll be returned to the system prompt.

4:18

Let's resize our console
window back where it was, and

4:23

bring our widgets.rb code back up.

4:26

So now let's see if we can use string
concatenation to fix our ask method.

4:30

As we mentioned, the question that we're
asking the user is running right up

4:34

against the space where they're
supposed to type their response.

4:37

We can fix this by concatenating a space
character on to the end of the question.

4:42

Let's try running this again now.

4:45

So we'll say ruby space widgets.rb.

4:51

And we'll get asked as before,
how many widgets are you ordering?

4:53

But notice that there's now a space
between the question and the cursor.