1 00:00:00,400 --> 00:00:04,679 Here I've created another file named temp.rb that contains some new Ruby code. 2 00:00:04,679 --> 00:00:08,965 Lines 1, 2, and 3 display a message that we're waiting, pause for 3 00:00:08,965 --> 00:00:13,259 3 seconds, and then print another message that we're done waiting. 4 00:00:13,259 --> 00:00:17,010 Lines 4, 5, and 6 count from 1 to 3. 5 00:00:17,010 --> 00:00:21,780 These are two separate tasks in the same program, but it's hard to tell at a glance 6 00:00:21,780 --> 00:00:26,060 which lines belong to which task, or what task they're supposed to be doing. 7 00:00:26,060 --> 00:00:28,680 It's easy to define your own methods in Ruby. 8 00:00:28,680 --> 00:00:31,270 You start with the def keyword, short for 9 00:00:31,270 --> 00:00:34,360 define, followed by the name of the method you want. 10 00:00:34,360 --> 00:00:37,400 So let's create a new method here named wait. 11 00:00:37,400 --> 00:00:39,960 And next we're going to need a method body, that's one or 12 00:00:39,960 --> 00:00:43,310 more lines of code that will be run when the method is called. 13 00:00:43,310 --> 00:00:47,150 The lines of a method body are usually indented to make it clear that they're 14 00:00:47,150 --> 00:00:51,290 part of the method, although this isn't required by the Ruby interpreter itself. 15 00:00:51,290 --> 00:00:53,260 But it's a very common standard practice. 16 00:00:53,260 --> 00:00:56,420 I would definitely recommend indenting your method lines. 17 00:00:56,420 --> 00:01:00,160 So we're just going to take these first three lines of existing code and 18 00:01:00,160 --> 00:01:02,920 put them inside a wait method. 19 00:01:02,920 --> 00:01:05,610 The end of the method is marked by the end keyword. 20 00:01:07,110 --> 00:01:10,975 And the end keyword should be aligned with the def keyword at the start of 21 00:01:10,975 --> 00:01:11,694 the method. 22 00:01:11,694 --> 00:01:15,562 So we'll convert those first three lines of code to a wait method. 23 00:01:15,562 --> 00:01:21,754 And now let's define a count_to_three method to hold the remaining code. 24 00:01:25,056 --> 00:01:27,617 We'll just take those existing lines of code and 25 00:01:27,617 --> 00:01:31,000 indent them to form the body of the second method. 26 00:01:31,000 --> 00:01:34,260 And again, we'll end the method with the end keyword. 27 00:01:34,260 --> 00:01:38,050 You don't get to use just any character you want in a method name. 28 00:01:38,050 --> 00:01:41,320 Generally speaking, method names should be all lowercase. 29 00:01:41,320 --> 00:01:45,990 You can add numbers into them, but those are rarely used, so try to avoid that. 30 00:01:45,990 --> 00:01:47,890 If there are multiple words in your method name, 31 00:01:47,890 --> 00:01:51,380 you should separate them with underscore characters. 32 00:01:51,380 --> 00:01:53,140 This style is called snake case, 33 00:01:53,140 --> 00:01:56,490 because it makes the name look like it's crawling on the ground. 34 00:01:56,490 --> 00:01:58,802 Now that we've defined the methods, we need to call them so 35 00:01:58,802 --> 00:02:00,197 that they're actually executed. 36 00:02:00,197 --> 00:02:03,110 We do that just like we did the predefined methods, 37 00:02:03,110 --> 00:02:05,838 we simply type the name of the method to call it. 38 00:02:05,838 --> 00:02:11,942 So count_to_three will call the count_to_three method that we've defined, 39 00:02:11,942 --> 00:02:16,055 and wait will call the wait method that we've defined. 40 00:02:16,055 --> 00:02:21,280 Let's hit Command + S to save that, Ctrl + S if you're on Windows. 41 00:02:21,280 --> 00:02:25,970 And click down in the console area to try running it again. 42 00:02:25,970 --> 00:02:27,630 Up arrow to bring up the previous command. 43 00:02:29,080 --> 00:02:33,290 And you can see that our call to count_to_three causes the count_to_three 44 00:02:33,290 --> 00:02:36,600 method to run and print the numbers 1, 2 and 3. 45 00:02:36,600 --> 00:02:43,620 And then the call to wait causes the wait method to run and print the message, 46 00:02:43,620 --> 00:02:47,650 waiting, then sleep for 3 seconds, and then print the message, done. 47 00:02:47,650 --> 00:02:50,880 We can call a method during a program as many times as we want. 48 00:02:50,880 --> 00:02:54,620 So let's add a couple additional calls to count_to_three down here. 49 00:02:54,620 --> 00:02:59,030 I'll just copy the first call and paste it in a couple more times, save my work. 50 00:03:01,050 --> 00:03:03,420 And re-run it from the console. 51 00:03:03,420 --> 00:03:07,445 And you can see that it calls count_to_three once. 52 00:03:07,445 --> 00:03:11,810 It calls wait once and then it calls count_to_three two more times down here. 53 00:03:13,080 --> 00:03:15,630 Now that we understand method calls a little bit better, 54 00:03:15,630 --> 00:03:21,000 let's go back to our widgets.rb file by clicking on it here on the side bar. 55 00:03:21,000 --> 00:03:23,820 And let's implement our welcome message to the user. 56 00:03:23,820 --> 00:03:26,531 We can do that with the single call to the puts method. 57 00:03:26,531 --> 00:03:33,481 We'll pass it a string with the message, Welcome to the widget store!, 58 00:03:33,481 --> 00:03:39,100 close the string with a pair of double quotes and save that. 59 00:03:39,100 --> 00:03:42,217 And then click down here in the console to run it. 60 00:03:42,217 --> 00:03:48,340 We do that with ruby widgets.rb. 61 00:03:48,340 --> 00:03:52,830 And you can see that it prints our welcome to the widget store message. 62 00:03:53,870 --> 00:03:56,880 Our program is displaying a welcome message to the user. 63 00:03:56,880 --> 00:03:59,260 We can cross the first requirement off our list.