1 00:00:00,370 --> 00:00:04,160 Our first requirement for the widget store program is to print a welcome message for 2 00:00:04,160 --> 00:00:05,360 the user. 3 00:00:05,360 --> 00:00:09,880 Before we can start coding, we're going to need a file to hold our Ruby source code. 4 00:00:09,880 --> 00:00:12,340 Ruby source code is stored in plain text files but 5 00:00:12,340 --> 00:00:17,280 instead of .txt, the file usually ends in a .rb extension. 6 00:00:17,280 --> 00:00:19,110 So to create a Ruby source code file, 7 00:00:19,110 --> 00:00:22,080 we're gonna go to the File menu, we'll choose New File. 8 00:00:22,080 --> 00:00:27,130 And we'll type a name for our file of widgets.rb, and 9 00:00:28,210 --> 00:00:33,450 that will create a widget.rb plain text file and open it in our editor. 10 00:00:33,450 --> 00:00:35,380 Now we need to print a welcome message. 11 00:00:35,380 --> 00:00:40,100 We can do that using the puts method which stands for put string. 12 00:00:40,100 --> 00:00:41,310 So let's try that now. 13 00:00:41,310 --> 00:00:46,231 We'll make a call to puts, and we'll pass it a string here within 14 00:00:46,231 --> 00:00:50,629 the quote marks, and we'll just say hello world for now. 15 00:00:51,952 --> 00:00:56,680 Go to your File menu, and choose Save when you're done to save your changes. 16 00:00:56,680 --> 00:01:01,900 To run a Ruby program, we need to run the Ruby command in the terminal or a console. 17 00:01:01,900 --> 00:01:04,370 So click down here in the Console area, and 18 00:01:04,370 --> 00:01:08,790 then type ruby, a space, and the name of the file you want to run. 19 00:01:08,790 --> 00:01:12,030 So, that's widgets.rb. 20 00:01:12,030 --> 00:01:14,040 As long as you're in the same folder, or 21 00:01:14,040 --> 00:01:18,220 directory as that file, then all you need to type is the file name. 22 00:01:18,220 --> 00:01:21,540 And you can see that down here, the program runs and 23 00:01:21,540 --> 00:01:23,560 it prints our message, hello world. 24 00:01:23,560 --> 00:01:27,240 If you're in a different folder or directory than the file you want to run, 25 00:01:27,240 --> 00:01:31,000 then you're going to need to change into that directory before running the file. 26 00:01:31,000 --> 00:01:37,445 So let's try creating a new folder here, we'll call it my_folder. 27 00:01:37,445 --> 00:01:39,372 And then we'll create a new file within it. 28 00:01:40,890 --> 00:01:43,940 And we'll call it other.rb. 29 00:01:43,940 --> 00:01:48,768 And we'll just do another simple program in here that prints out other file. 30 00:01:50,680 --> 00:01:53,095 Go to the File menu and choose Save. 31 00:01:53,095 --> 00:01:59,011 And by the way a shortcut, if you want is you can hold down the Cmd key on MacOS, 32 00:01:59,011 --> 00:02:03,560 or the Ctrl key on Windows and press S to save the file. 33 00:02:03,560 --> 00:02:06,010 We'll be doing that a lot for the rest of the course. 34 00:02:06,010 --> 00:02:11,050 So now we have this file named other.rb saved in a folder named my_folder. 35 00:02:11,050 --> 00:02:16,870 If I try going down here clicking in the Console and running ruby other.rb, 36 00:02:16,870 --> 00:02:22,170 it won't work, because there's no file named other.rb in the current directory. 37 00:02:22,170 --> 00:02:28,200 Instead, we need to change directories using the cd or change directory command. 38 00:02:28,200 --> 00:02:31,220 And we need to type in the name of the directory or 39 00:02:31,220 --> 00:02:33,570 folder that we want to change into. 40 00:02:33,570 --> 00:02:36,940 So we'll change into the folder named my folder. 41 00:02:36,940 --> 00:02:41,339 And now that we're in this other directory, we can go ahead and 42 00:02:41,339 --> 00:02:45,745 run the other.rb program and Ruby will find it successfully. 43 00:02:46,791 --> 00:02:49,590 And there you see it prints our message, other file. 44 00:02:49,590 --> 00:02:51,560 But we don't need to do any of that right now, 45 00:02:51,560 --> 00:02:53,540 we're okay working in just the one folder. 46 00:02:53,540 --> 00:02:58,118 So I'm going to change back to the parent directory using this special notation 47 00:02:58,118 --> 00:03:01,943 here, .., which means the parent of the current directory that 48 00:03:01,943 --> 00:03:04,977 is the directory that holds the current directory. 49 00:03:04,977 --> 00:03:09,500 And I'll hit Enter, so that I'm back in the workspace folder where I began. 50 00:03:09,500 --> 00:03:11,688 And now I'm going to delete my_folder. 51 00:03:13,390 --> 00:03:16,929 And that I'll delete the other.rb file along with it. 52 00:03:16,929 --> 00:03:22,140 Now let's take a closer look at the code within our widgets.rb file. 53 00:03:22,140 --> 00:03:23,660 Puts is a method, 54 00:03:23,660 --> 00:03:27,720 a group of code statements that together perform a particular task. 55 00:03:27,720 --> 00:03:31,450 Puts stands for put string, and that's the task it carries out. 56 00:03:31,450 --> 00:03:35,070 It puts a string of text characters on the terminal output. 57 00:03:35,070 --> 00:03:39,590 Typing the method name here calls that method or causes it to run. 58 00:03:39,590 --> 00:03:41,940 The text here inside quotes is a string. 59 00:03:41,940 --> 00:03:44,190 We'll be talking more about string soon. 60 00:03:44,190 --> 00:03:47,160 Because we are putting the string here following the method call, 61 00:03:47,160 --> 00:03:49,740 it's passed as an argument to the method. 62 00:03:49,740 --> 00:03:52,720 Methods take the arguments that are provided when they're called and 63 00:03:52,720 --> 00:03:54,415 work with them in some way. 64 00:03:54,415 --> 00:03:58,880 Puts just prints whatever arguments it receives out to the terminal. 65 00:03:58,880 --> 00:04:00,880 Let's add a few different method calls here so 66 00:04:00,880 --> 00:04:02,720 you can see what I'm talking about. 67 00:04:02,720 --> 00:04:06,040 First, we'll make another call to puts, and this time, 68 00:04:06,040 --> 00:04:09,510 we're going to type parenthesis following the method name. 69 00:04:09,510 --> 00:04:12,010 We'll pass at several arguments. 70 00:04:12,010 --> 00:04:16,130 If you're passing multiple arguments to a method, you separate them with commas. 71 00:04:16,130 --> 00:04:18,723 So we'll pass a couple of numbers here as arguments, and 72 00:04:18,723 --> 00:04:20,623 then we'll pass a couple of strings too. 73 00:04:22,410 --> 00:04:25,469 And then we type a closing parenthesis to end the method call. 74 00:04:26,870 --> 00:04:31,257 Now let's make another call to a different method, one called sleep. 75 00:04:31,257 --> 00:04:34,620 What sleep does is it causes the program to pause execution for 76 00:04:34,620 --> 00:04:37,100 the number of seconds you specify. 77 00:04:37,100 --> 00:04:40,050 So you pass an argument with number of seconds that you 78 00:04:40,050 --> 00:04:42,010 want the program to sleep for. 79 00:04:42,010 --> 00:04:43,570 Now let's add another method call. 80 00:04:43,570 --> 00:04:46,190 This one to the print method. 81 00:04:46,190 --> 00:04:51,240 Print works a lot like puts except that while puts skips to a new line 82 00:04:51,240 --> 00:04:55,540 after each new argument that it receives, print just stays on the same line. 83 00:04:55,540 --> 00:04:58,520 So we'll pass a couple more string arguments to print. 84 00:04:58,520 --> 00:05:00,100 Let's say, c and d. 85 00:05:01,200 --> 00:05:05,133 And then let's make one more call to sleep, and we'll just have it sleep for 86 00:05:05,133 --> 00:05:06,308 one second this time. 87 00:05:06,308 --> 00:05:10,666 We'll hit Cmd+S to save this, Ctrl+S if you're on a Windows machine. 88 00:05:10,666 --> 00:05:14,280 And now let's click down here on the Console, and run the program again. 89 00:05:14,280 --> 00:05:17,627 We'll hit the up arrow to bring the previous command back and 90 00:05:17,627 --> 00:05:18,946 run ruby widgets.rb. 91 00:05:22,001 --> 00:05:23,290 And you see that as before, 92 00:05:23,290 --> 00:05:27,300 it makes a call to puts with the string hello world and it prints that out. 93 00:05:27,300 --> 00:05:31,687 And then comes our call to puts with the numbers 1 and 2, and 94 00:05:31,687 --> 00:05:34,320 the low strings a and b. 95 00:05:34,320 --> 00:05:38,560 And you can see that each of those prints on a separate line down here. 96 00:05:38,560 --> 00:05:42,710 If we rerun the program again, you'll notice that after it prints one 1, 2 a, 97 00:05:42,710 --> 00:05:47,950 b, it pauses for a couple seconds before printing c and d here on the same line. 98 00:05:47,950 --> 00:05:52,190 That pause was due to the call to sleep with an argument of 2, 99 00:05:52,190 --> 00:05:54,610 it slept for 2 seconds. 100 00:05:54,610 --> 00:05:59,160 Then the call to print printed the string c and d on the same line. 101 00:05:59,160 --> 00:06:02,990 And finally, the program slept for one more second before ending. 102 00:06:02,990 --> 00:06:05,810 Parentheses are optional on Ruby method calls. 103 00:06:05,810 --> 00:06:10,000 So, you'll notice that we made a call to puts up here without any parentheses 104 00:06:10,000 --> 00:06:10,650 surrounding it. 105 00:06:10,650 --> 00:06:14,730 We just put a space between the method name and its one argument. 106 00:06:14,730 --> 00:06:20,020 We can actually remove parentheses from all of these method it calls down here, 107 00:06:20,020 --> 00:06:22,380 and they'll still run in the same way. 108 00:06:22,380 --> 00:06:26,306 So I'm going to replace the opening parenthesis each with a space, and 109 00:06:26,306 --> 00:06:29,456 I will just delete the closing parenthesis on each line. 110 00:06:29,456 --> 00:06:33,597 Hit Cmd+S to save that, Ctrl+S if you are on Windows. 111 00:06:33,597 --> 00:06:35,263 And let's rerun the program again and 112 00:06:35,263 --> 00:06:37,250 you will see that it runs in just the same way.