1 00:00:00,430 --> 00:00:02,158 Let's get back to our Widget Store program. 2 00:00:02,158 --> 00:00:05,972 We're still working on asking the user how many widgets they want to order and 3 00:00:05,972 --> 00:00:07,567 storing the number they enter. 4 00:00:07,567 --> 00:00:11,168 Now that we know how to write methods and pass arguments to them, 5 00:00:11,168 --> 00:00:13,845 we know most of what we need to add this feature. 6 00:00:13,845 --> 00:00:14,945 Let's give it a try now. 7 00:00:14,945 --> 00:00:19,704 We'll define a new ask method that takes a question 8 00:00:19,704 --> 00:00:22,923 parameter in the form of a string. 9 00:00:22,923 --> 00:00:27,701 Make sure to type an end keyword to end your method definition. 10 00:00:27,701 --> 00:00:34,470 And for now let's simply call the print method within the ask method body and 11 00:00:34,470 --> 00:00:38,510 pass it the question, we got as a parameter. 12 00:00:38,510 --> 00:00:41,707 And following our welcome to the widget store method, 13 00:00:41,707 --> 00:00:44,151 let's make a call to this new ask method. 14 00:00:44,151 --> 00:00:49,476 We'll call ask and then here in parentheses we'll pass a string. 15 00:00:49,476 --> 00:00:56,184 And in that string, we'll say, how many widgets are you ordering? 16 00:00:59,503 --> 00:01:04,411 Let's save that and try running it with ruby widgets.rb. 17 00:01:04,411 --> 00:01:07,999 And we can see our output down here in the terminal. 18 00:01:07,999 --> 00:01:11,147 As before we've got our welcome message welcome at the widget store. 19 00:01:11,147 --> 00:01:15,445 And then here's the results of our call to the ask method up here. 20 00:01:15,445 --> 00:01:18,062 We pass it the string how many widgets are you ordering? 21 00:01:18,062 --> 00:01:20,023 And that gets printed down here. 22 00:01:20,023 --> 00:01:24,333 Notice that it's not skipping to a new line after printing the question that's 23 00:01:24,333 --> 00:01:27,883 because we're calling the print method instead of put S method. 24 00:01:27,883 --> 00:01:29,847 The reason for that will become clear shortly. 25 00:01:29,847 --> 00:01:34,405 Now we need to get the text that the user enters at the keyboard, 26 00:01:34,405 --> 00:01:36,904 the gets method can do that for us. 27 00:01:36,904 --> 00:01:40,460 When we call gets it waits for the user to type something and 28 00:01:40,460 --> 00:01:43,522 press enter and then returns what the user types. 29 00:01:43,522 --> 00:01:45,452 In this version of ask, 30 00:01:45,452 --> 00:01:50,771 we'll just assign the value the user typed to a variable answer. 31 00:01:50,771 --> 00:01:55,760 We'll name our variable answer and we'll assign it the return value from gets. 32 00:01:55,760 --> 00:01:58,481 And then we'll simply print that value. 33 00:02:01,685 --> 00:02:03,286 So we'll save that and try running it. 34 00:02:03,286 --> 00:02:09,673 And as before, it'll print out the question that it's asking us. 35 00:02:09,673 --> 00:02:11,042 How many widgets are you ordering? 36 00:02:11,042 --> 00:02:14,579 And now it's waiting for us to type some input in the keyboard. 37 00:02:14,579 --> 00:02:19,090 And that's why we used a print method to print the question instead of puts which 38 00:02:19,090 --> 00:02:20,991 would skip down to the next line. 39 00:02:20,991 --> 00:02:25,226 Our input is going to appear here on the same line as the question. 40 00:02:25,226 --> 00:02:28,595 So now we can type whatever we want here at the prompt. 41 00:02:28,595 --> 00:02:32,824 So I'll use a value of 8 and press Enter when I'm done. 42 00:02:32,824 --> 00:02:37,155 And the value that I typed will get returned from the gets method, 43 00:02:37,155 --> 00:02:41,487 assigned to the answer variable, and then that will get passed to 44 00:02:41,487 --> 00:02:45,932 the puts method called down here, and printed out to the console. 45 00:02:45,932 --> 00:02:49,161 So we typed 8, and it prints 8. 46 00:02:49,161 --> 00:02:51,729 So how does this work exactly? 47 00:02:51,729 --> 00:02:53,952 Well, Ruby methods have a return value, 48 00:02:53,952 --> 00:02:56,948 a value that they send back to the code that called them. 49 00:02:56,948 --> 00:03:02,035 Here, the return value from gets is getting assigned to the variable answer. 50 00:03:02,035 --> 00:03:05,602 Let's take a look at return values in another context though. 51 00:03:05,602 --> 00:03:08,911 Here are our add and subtract methods from before. 52 00:03:08,911 --> 00:03:13,492 First I'm going to get rid of this first line here, which prints out the parameters 53 00:03:13,492 --> 00:03:16,970 that they receive cuz we don't really need that code anymore. 54 00:03:16,970 --> 00:03:21,536 Now currently the add and subtract methods just print the result of adding or 55 00:03:21,536 --> 00:03:24,547 subtracting their first and second parameters. 56 00:03:24,547 --> 00:03:28,665 But let's modify these methods so that they use a return value instead. 57 00:03:28,665 --> 00:03:32,906 I'm going to remove the call to puts and then in its place, 58 00:03:32,906 --> 00:03:36,073 I'm going to use the return keyword. 59 00:03:36,073 --> 00:03:40,887 Following the return keyword is an argument that you want to 60 00:03:40,887 --> 00:03:43,302 return to the method caller. 61 00:03:43,302 --> 00:03:47,139 So in this case, we'll just use the exact same code that we did previously. 62 00:03:47,139 --> 00:03:50,658 We'll add the first and second parameters and return that value. 63 00:03:50,658 --> 00:03:56,456 Down here I'll remove the call to puts and I'll use a return keyword there as well. 64 00:03:56,456 --> 00:03:58,845 Let's try running this. 65 00:04:02,888 --> 00:04:07,676 Ruby parameters.rb and we don't get any output right at first that's 66 00:04:07,676 --> 00:04:11,187 because the return keyword doesn't print a value or 67 00:04:11,187 --> 00:04:15,201 do anything besides pass it back to the code that called it. 68 00:04:15,201 --> 00:04:19,972 In order to actually see the output again we're going to need to do something with 69 00:04:19,972 --> 00:04:24,617 those return values down here in the place that we're calling these methods. 70 00:04:24,617 --> 00:04:28,907 So this call to the add method here is actually returning a value right now. 71 00:04:28,907 --> 00:04:32,492 It's calling the result of adding its first and second parameters. 72 00:04:32,492 --> 00:04:35,460 But we're not actually doing anything with that return value. 73 00:04:35,460 --> 00:04:40,289 So I'm going to take the return value and pass it to puts method down here. 74 00:04:40,289 --> 00:04:42,966 If we save that and try running it, 75 00:04:42,966 --> 00:04:45,036 this time we'll see output. 76 00:04:45,036 --> 00:04:49,263 We'll see the return value that we're getting from the add function, 150. 77 00:04:49,263 --> 00:04:51,752 Let's do the same for the remaining method calls. 78 00:04:51,752 --> 00:04:57,052 So puts subtract, puts add, puts subtract. 79 00:04:57,052 --> 00:04:59,196 Try running it again. 80 00:04:59,196 --> 00:05:04,754 And this time you can see that all four of our method calls are returning values and 81 00:05:04,754 --> 00:05:07,627 then we're printing them out down here. 82 00:05:07,627 --> 00:05:10,503 We don't actually need the return keywords here though. 83 00:05:10,503 --> 00:05:15,172 The last expression evaluated in a method becomes that method's return value. 84 00:05:15,172 --> 00:05:20,116 So if we were to remove the return keyword here, first plus second would be 85 00:05:20,116 --> 00:05:24,425 the last expression evaluated within the add method's body, and 86 00:05:24,425 --> 00:05:27,720 that would become the method's return value. 87 00:05:27,720 --> 00:05:30,577 So let's remove the return keyword from add, and 88 00:05:30,577 --> 00:05:33,639 we can remove the return keyword from subtract as well 89 00:05:33,639 --> 00:05:37,737 because this expression will be the return value of the subtract method. 90 00:05:37,737 --> 00:05:44,045 And if we try running this, it'll behave in just the same way it did before. 91 00:05:44,045 --> 00:05:48,223 There's a variety of things you can do with Ruby return values. 92 00:05:48,223 --> 00:05:51,678 You can either take the return value and pass it directly to another 93 00:05:51,678 --> 00:05:55,331 method as we're doing with puts in our add and subtract methods here. 94 00:05:55,331 --> 00:05:59,408 Or you can assign a return value to a variable first, if you prefer. 95 00:05:59,408 --> 00:06:04,352 So for example, we can assign the return value from add here to 96 00:06:04,352 --> 00:06:08,646 a total variable, and then print that out if we want. 97 00:06:08,646 --> 00:06:09,766 Save that, try running it. 98 00:06:09,766 --> 00:06:13,704 And you can see that the end result is the same here. 99 00:06:13,704 --> 00:06:16,432 We take our first call to add. 100 00:06:16,432 --> 00:06:18,705 Its return value is going to be 150. 101 00:06:18,705 --> 00:06:21,627 We assign that to the total variable and then print that out here. 102 00:06:21,627 --> 00:06:24,287 And you can see a 150 in our results down here. 103 00:06:24,287 --> 00:06:29,024 We can also use method return values as arguments for our own methods. 104 00:06:29,024 --> 00:06:34,538 So for example, I can take the return value from calling add 1 and 105 00:06:34,538 --> 00:06:39,662 2, and I can pass that back to the add method as an argument. 106 00:06:39,662 --> 00:06:46,150 So add 1 and 2 will give us three, we can add 3 and 4 like this. 107 00:06:46,150 --> 00:06:49,398 Let's try saving that, and running it. 108 00:06:52,176 --> 00:06:54,535 Whoops, I forgot to call puts with that return value. 109 00:06:54,535 --> 00:06:56,664 Let me go back up into the code and have that there. 110 00:06:56,664 --> 00:06:58,483 And that's we had no output. 111 00:06:58,483 --> 00:06:59,893 Let's see if we get output this time. 112 00:06:59,893 --> 00:07:01,121 There we go. 113 00:07:01,121 --> 00:07:07,539 The result of adding 1 and 2 is 3, and the result of adding that to 4 is 7. 114 00:07:07,539 --> 00:07:09,660 You can see that in the output down here. 115 00:07:09,660 --> 00:07:12,676 Let's try making another call to add. 116 00:07:12,676 --> 00:07:19,583 And this time we'll take the number 3 and we'll add the results of subtracting. 117 00:07:22,793 --> 00:07:26,033 11 and 7, 118 00:07:26,033 --> 00:07:30,749 try running that. 119 00:07:30,749 --> 00:07:36,431 And the results of subtracting 7 from 11 is 4 if we add that to 3, 120 00:07:36,431 --> 00:07:38,438 the result is 7 again. 121 00:07:38,438 --> 00:07:43,143 It's very important not to confuse a method returning a value 122 00:07:43,143 --> 00:07:45,956 with if printing a value using puts. 123 00:07:45,956 --> 00:07:50,844 So let's take our subtract method here and let's pass a variable to it. 124 00:07:50,844 --> 00:07:55,552 We'll declare a variable named number and we'll assign it a value of 9. 125 00:07:55,552 --> 00:07:58,220 We'll print out of the value it contains. 126 00:07:58,220 --> 00:08:06,188 And then we'll reassign to that variable the result of calling the subtract method. 127 00:08:10,204 --> 00:08:12,374 A little typo there, let me correct that. 128 00:08:12,374 --> 00:08:16,642 And we'll call the subtract method with the value currently in number. 129 00:08:16,642 --> 00:08:22,635 And the number 1, which will subtract 1 from whatever's in number and 130 00:08:22,635 --> 00:08:25,001 assign that back to number. 131 00:08:25,001 --> 00:08:29,781 So number should contain 8 after this, and let's copy and 132 00:08:29,781 --> 00:08:33,900 paste that code so that we do it again a second time. 133 00:08:33,900 --> 00:08:37,427 And then with printing the final value in the number variable. 134 00:08:37,427 --> 00:08:39,527 Okay, let's try running this. 135 00:08:42,737 --> 00:08:45,482 And we see 9, 8, 7 as our output. 136 00:08:45,482 --> 00:08:49,535 We start with 9, subtract 1 and get 8, subtract 1 again and get 7. 137 00:08:49,535 --> 00:08:53,333 But suppose that instead of returning a value, the subtract method 138 00:08:53,333 --> 00:08:57,344 printed the result of subtracting its second argument from its first. 139 00:08:57,344 --> 00:09:02,193 This will actually return an empty value from the subtract method. 140 00:09:02,193 --> 00:09:07,151 So lets try running this, and it appears to work correctly at first. 141 00:09:07,151 --> 00:09:10,174 It prints out the value and our number variable 9, and 142 00:09:10,174 --> 00:09:14,199 then it appears to subtract 1 from it giving us 8 and printing that out. 143 00:09:14,199 --> 00:09:16,516 But what's actually happening here, 144 00:09:16,516 --> 00:09:19,613 is that the call to puts is returning an empty value. 145 00:09:19,613 --> 00:09:22,696 And that's what gets assigned to the number variable down here. 146 00:09:22,696 --> 00:09:28,166 Then when we try to call subtract with number, which now contains this empty 147 00:09:28,166 --> 00:09:33,897 value, we get an undefined method minus for NilClass, that is an empty value. 148 00:09:33,897 --> 00:09:38,664 So make sure not to confuse a method calling puts with returning a value. 149 00:09:38,664 --> 00:09:43,674 In many cases a method will only work correctly if it actually returns a value. 150 00:09:43,674 --> 00:09:47,863 So now that we actually understand method return values a bit better, 151 00:09:47,863 --> 00:09:50,350 let's update our ask method to use them. 152 00:09:50,350 --> 00:09:53,274 Instead of taking the value return from gets and 153 00:09:53,274 --> 00:09:57,221 assigning it to a variable and then printing that variable out, 154 00:09:57,221 --> 00:10:00,969 let's simply return the user's input from the ask method. 155 00:10:00,969 --> 00:10:06,098 We'll assign that return value to a variable named answer down here. 156 00:10:06,098 --> 00:10:10,856 And then after that we'll print the value that the answer variable holds. 157 00:10:10,856 --> 00:10:15,630 This way we'll be able to do other things with the user's response out 158 00:10:15,630 --> 00:10:17,237 here in the main code. 159 00:10:17,237 --> 00:10:19,824 Let's save then and try running it. 160 00:10:22,262 --> 00:10:26,952 Ruby widets.rb and it'll ask us how many widgets are you ordering? 161 00:10:26,952 --> 00:10:27,742 Let's say 8. 162 00:10:27,742 --> 00:10:33,174 And as before it will print the value that we input of 8. 163 00:10:33,174 --> 00:10:36,979 We've completed the second feature for our program. 164 00:10:36,979 --> 00:10:41,412 We ask the user which quantity of widgets they want, call the gets method to get 165 00:10:41,412 --> 00:10:45,659 their keyboard entry, and store the return value from gets in a variable. 166 00:10:45,659 --> 00:10:49,596 Learning the call methods was the key to getting this far in your program. 167 00:10:49,596 --> 00:10:53,084 Practicing your new skills is important to making them stick. 168 00:10:53,084 --> 00:10:57,394 So be sure to check the teacher's notes on this video for some practice ideas. 169 00:10:57,394 --> 00:11:00,568 You're off to a great start learning Ruby. 170 00:11:00,568 --> 00:11:03,862 You've learned the call methods and to use their return values. 171 00:11:03,862 --> 00:11:05,988 You've even learned to write your own methods. 172 00:11:05,988 --> 00:11:10,051 Those skills would be useful in pretty much every program you write. 173 00:11:10,051 --> 00:11:13,760 And now, you can also store values in variables when you need to, 174 00:11:13,760 --> 00:11:16,401 that's another skill you'll be using a lot. 175 00:11:16,401 --> 00:11:19,911 There's just one minor issue with the program. 176 00:11:19,911 --> 00:11:22,767 When we ask the user how many widgets they want to buy, 177 00:11:22,767 --> 00:11:26,849 our question runs right up against the place their keyboard input is shown. 178 00:11:26,849 --> 00:11:29,733 That looks a little less than professional. 179 00:11:29,733 --> 00:11:33,678 To fix this, we're going to need to learn more about strings. 180 00:11:33,678 --> 00:11:36,768 Just like most other programming languages, 181 00:11:36,768 --> 00:11:38,991 Ruby uses strings to store text. 182 00:11:38,991 --> 00:11:41,504 We'll learn all about them in the next stage. 183 00:11:41,504 --> 00:11:42,550 See you there.