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 0:00 right up against the space where they're suppose to type their answer. 0:04 In order to fix this, we're going to be to take the question that 0:07 we're asking the user, and add a space on to the end of it. 0:10 We can do this through ruby string concatenation or the joining of strings. 0:13 We'll show you how to do string concatenation in our main program 0:18 in a bit, but first let's try it out in a different way. 0:22 I want to show you a separate program that gets installed along with Ruby called irb. 0:25 irb stands for interactive Ruby and we can launch it by clicking down in our console 0:31 and typing the letters irb and pressing Enter. 0:36 When we run irb it'll show you a prompt where you can 0:39 type Ruby expressions one at a time hitting Enter after each. 0:42 Irb will immediately show you the result of each expression, 0:50 you don't need to call puts or anything. 0:53 It's a great way to try code out and see what it does, and 0:55 even professional Ruby developers use it as a way to quickly test out ideas. 0:58 So now that we know how irb works, let's use it to try out string concatenation. 1:02 I'm gonna resize the console so that it has a little more room on the screen. 1:07 You concatenate strings together using the plus operator. 1:13 So let's try typing one string, the plus operator and 1:16 a second string that we wanna join onto it. 1:20 You can see the result is the concatenated string, ab. 1:22 Let's try that again with slightly longer strings. 1:26 So we'll try a string that consists of some words + more words. 1:29 And you can see that they got joined together without any spaces between them. 1:38 That's something you need to be careful of if you're using actual English words. 1:41 You need to be sure to include spaces in the concatenated version. 1:47 So we'll concatenate three strings together. 1:53 Our first string, a string consisting of a single space and our second string. 1:56 And now, everything's spaced properly. 2:03 If you concatenate one string onto another that's stored in a variable, 2:05 it won't affect the string that's stored in the variable. 2:09 Let's try creating a variable named myvar, and we'll store a string in it. 2:12 And now, let's try concatenating another 2:20 string onto the string in myvar, myvar + abc. 2:25 And you can see that the result is concatenated string, a string abc. 2:30 But if we take a look at the contents of myvar, which in irb you can just 2:35 type myvar and it will print what myvar contains for you. 2:40 You can see that myvar is ineffective, it still contains just a string. 2:46 To concatenate the string and actually change the value that's held in 2:51 the variable, we can use an abbreviated assignment operator. 2:54 We'll talk about those operators more later, but 2:58 let's just do a quick demonstration. 3:01 So myvar, and we use the abbreviated += 3:03 assignment operator, and we'll concatenate the same string on as we did before. 3:07 myvar += abc. 3:12 And you can see the result is a string abc. 3:14 But if we type just myvar to look at its contents, 3:17 we can see that its contents have been updated as well. 3:21 And if we did that again with a different string, if we say myvar += def, we can see 3:23 that another string has been concatenated on to the end of the first one. 3:30 And that the contents of myvar have been updated with that as well. 3:34 We now have a string, the first concatenated string abc and 3:39 the second concatenated string def. 3:43 Strings can only be concatenated together with other strings. 3:46 Anything else like a number will result in an error. 3:50 So if we were to take the number 1 and 3:53 try to concatenate a string on to the end of it, we'll get an error. 3:56 We'll also get an error if we take a string and 4:02 try to concatenate a number onto that. 4:06 We'll be showing you a solution for this situation shortly. 4:09 When you're done and you're ready to exit irb, type exit and 4:12 press Enter, you'll be returned to the system prompt. 4:15 Let's resize our console window back where it was, and 4:18 bring our widgets.rb code back up. 4:23 So now let's see if we can use string concatenation to fix our ask method. 4:26 As we mentioned, the question that we're asking the user is running right up 4:30 against the space where they're supposed to type their response. 4:34 We can fix this by concatenating a space character on to the end of the question. 4:37 Let's try running this again now. 4:42 So we'll say ruby space widgets.rb. 4:45 And we'll get asked as before, how many widgets are you ordering? 4:51 But notice that there's now a space between the question and the cursor. 4:53 Now let's try typing our response as we did before, and 4:59 you'll notice that it's spaced properly now, thanks to string concatenation. 5:03 It looks like there's another improvement we can make here. 5:07 Right now, we're just printing out whatever the user enters with no 5:10 explanation. 5:13 So let's incorporate that into a more readable message. 5:14 Instead of puts answer, let's say, puts, 5:17 You entered, and concatenate that with answer, 5:22 and concatenate that with widgets. 5:27 So if they enter 8 widgets it'll say, you entered 8 widgets. 5:32 Let's try running this again. 5:37 But we noticed there is a problem. 5:42 We forgot to add spaces surrounding answer here so we wind up with 5:44 you entered running right up against the user's answer here in the output. 5:48 So let's go back into the code and add spaces surrounding the answer variable. 5:52 So You entered space, answer space widgets, and we'll enter 8 again. 5:57 There's a space here before the users answer, and we can see another space 6:05 down here on the second line, but why is there a line break in the middle of this? 6:09 You may also be wondering why we didn't get an error, 6:13 since strings can only be concatenated with other strings. 6:16 The reason is, the value in the answer variable is a string. 6:19 The gets method always returns strings, so 6:23 even though the user entered a number, it's treated as a string. 6:26 Eventually, we'll have to convert it to an actual number, 6:30 which we'll see how to do later. 6:32 We'll also see how to fix it skipping to a new line after printing the answer. 6:34
You need to sign up for Treehouse in order to download course files.Sign up