1 00:00:00,120 --> 00:00:03,710 In order to avoid all these strings being crammed together without any space 2 00:00:03,710 --> 00:00:06,190 between, we have to remember to add a space 3 00:00:06,190 --> 00:00:09,040 to the strings on either side of our answer variable. 4 00:00:09,040 --> 00:00:10,660 This is a little awkward. 5 00:00:10,660 --> 00:00:14,310 Another problem that we encounter with string concatenation earlier is that if 6 00:00:14,310 --> 00:00:18,910 you try to concatenate the string and a number together, you'll get errors. 7 00:00:18,910 --> 00:00:23,770 So if we were to take a string, and try to concatenate a number with it, and 8 00:00:23,770 --> 00:00:32,940 try to run this, We'll see an error down here at the bottom. 9 00:00:35,760 --> 00:00:39,252 Ruby provides a feature called string interpolation that lets you 10 00:00:39,252 --> 00:00:43,110 substitute the result of Ruby code into the middle of a string. 11 00:00:43,110 --> 00:00:46,690 Interpolation can address these problems and more. 12 00:00:46,690 --> 00:00:49,300 Let's go to our console and launch irb, and 13 00:00:49,300 --> 00:00:52,270 let's try interpolating a value into a string. 14 00:00:52,270 --> 00:00:56,110 Interpolation works only within double quoted Ruby strings. 15 00:00:56,110 --> 00:01:00,250 Anywhere you want within a string, you can include an interpolation marker. 16 00:01:00,250 --> 00:01:02,230 A marker consists of a hash mark, 17 00:01:02,230 --> 00:01:07,350 also known as pound sign, an opening curly brace, and a closing curly brace. 18 00:01:09,780 --> 00:01:13,320 So let's close out the string here and try running this. 19 00:01:13,320 --> 00:01:17,620 And you can see that the interpolation marker was removed from the finish string. 20 00:01:18,810 --> 00:01:22,570 You can include any Ruby code you want between the curly braces of 21 00:01:22,570 --> 00:01:23,820 an interpolation marker. 22 00:01:26,840 --> 00:01:31,000 So let's include the method called Time.now 23 00:01:31,000 --> 00:01:34,600 here in this interpolation marker, close out the string. 24 00:01:34,600 --> 00:01:37,600 The code within the interpolation marker will be evaluated. 25 00:01:37,600 --> 00:01:40,950 The result will be converted to a string, and the resulting string 26 00:01:40,950 --> 00:01:45,280 will be substituted for the interpolation marker within the containing string. 27 00:01:45,280 --> 00:01:48,430 So you can see we have the start of our string here, and 28 00:01:48,430 --> 00:01:53,130 then we have the result of Time.now before the string finishes out. 29 00:01:53,130 --> 00:01:57,380 String concatenation lets us easily concatenate values like numbers together 30 00:01:57,380 --> 00:01:58,300 with strings. 31 00:01:58,300 --> 00:02:02,960 So let's say string put in interpolation marker into the middle of it, and 32 00:02:02,960 --> 00:02:06,720 we can include the number 1 without any problems. 33 00:02:06,720 --> 00:02:10,210 It also lets us embed simple math operations within a string. 34 00:02:10,210 --> 00:02:14,380 So we could say the answer is, 35 00:02:16,680 --> 00:02:21,708 And include an interpolation marker with the operation 1 + 2. 36 00:02:21,708 --> 00:02:25,520 Close out the string, and we get the answer is 3. 37 00:02:25,520 --> 00:02:29,410 interpolation is also an easy way to invent the values of variables. 38 00:02:29,410 --> 00:02:33,040 So lets say we had a name variable that is set to "Jay", 39 00:02:33,040 --> 00:02:38,290 we could interpolate that into a string with Hello interpolation marker, 40 00:02:39,890 --> 00:02:46,190 the variable name, And close out the string, and we get "Hello Jay!". 41 00:02:46,190 --> 00:02:50,010 We can include multiple interpolation markers into a single string. 42 00:02:50,010 --> 00:02:53,970 So we can say, {Time.now}, 43 00:02:53,970 --> 00:02:59,890 followed by the value of {name}, and we get the two together as a single string. 44 00:03:01,760 --> 00:03:06,070 If you ever actually want to include pound signs and curly braces in your output, 45 00:03:06,070 --> 00:03:09,450 you can use single quoted strings to quote them literally. 46 00:03:09,450 --> 00:03:14,351 So if we were to say Hello #{name} and end the string, 47 00:03:14,351 --> 00:03:19,980 we'll see those literal characters within the output. 48 00:03:19,980 --> 00:03:23,550 As usual, when you're ready to exit irb and type exit and 49 00:03:23,550 --> 00:03:26,400 press Enter, you'll be returned to the system prompt. 50 00:03:26,400 --> 00:03:30,570 Let's try and improving our widget store code using string interpolation. 51 00:03:30,570 --> 00:03:35,170 We'll remove the plus signs to combine all the concatenated strings into one. 52 00:03:35,170 --> 00:03:36,030 In their place, 53 00:03:36,030 --> 00:03:40,310 we'll have an interpolation market that inserts the value of the answer variable. 54 00:03:42,520 --> 00:03:46,100 It's easy to remember to put spaces surrounding the interpolation marker, 55 00:03:46,100 --> 00:03:47,920 because it would look funny if we left them out. 56 00:03:49,590 --> 00:03:51,420 Let's try saving this and running it. 57 00:03:56,700 --> 00:04:00,645 And you can see in the output that it's inserting the value of the answer 58 00:04:00,645 --> 00:04:04,750 variable into the string, and it is surrounded by spaces as it should be. 59 00:04:06,060 --> 00:04:09,020 But, why does it skip to a new line here? 60 00:04:09,020 --> 00:04:09,840 To figure that out, 61 00:04:09,840 --> 00:04:13,910 we're going to need to take a closer look at the value of the answer variable. 62 00:04:13,910 --> 00:04:16,960 And for that, we'll need to take a brief detour in the next video.