Methods6:38 with Jay McGavren
Our first requirement is to print a welcome message for the user. To do that, we'll need to learn how to call "methods".
Ruby source files
- Ruby source code is stored in plain text files, but instead of
.txt, the file usually ends in
- To run a Ruby program, we need to run the
rubycommand in the terminal or console.
ruby, a space, and the name of the file you want to run. As long as you're in the same folder or directory as that file, then all you need to type is the file name.
- To run a Ruby program, we need to run the
If you have a script named
other.rb that's in a folder named
my_folder, you have two ways to run it:
cd my_folder, then
puts "hello world"
putsis a method, a group of code statements that together perform a particular task.
putsstands for "put string", and that's the task it carries out: it puts a string of text characters on the terminal output.
- Typing the method name in your code calls that method, or causes it to run.
- The text inside quotes is a string. We'll be talking more about strings soon.
- Because we're putting the string here following the method call, it's passed as an argument to the method.
puts(1, 2, "a", "b") sleep(2) print(1, 2, "a", "b") sleep(1)
- Parentheses are optional
puts 1, 2, "a", "b" sleep 2 print 1, 2, "a", "b" sleep 1
Our first requirement for the widget store program is to print a welcome message for 0:00 the user. 0:04 Before we can start coding, we're going to need a file to hold our Ruby source code. 0:05 Ruby source code is stored in plain text files but 0:09 instead of .txt, the file usually ends in a .rb extension. 0:12 So to create a Ruby source code file, 0:17 we're gonna go to the File menu, we'll choose New File. 0:19 And we'll type a name for our file of widgets.rb, and 0:22 that will create a widget.rb plain text file and open it in our editor. 0:28 Now we need to print a welcome message. 0:33 We can do that using the puts method which stands for put string. 0:35 So let's try that now. 0:40 We'll make a call to puts, and we'll pass it a string here within 0:41 the quote marks, and we'll just say hello world for now. 0:46 Go to your File menu, and choose Save when you're done to save your changes. 0:51 To run a Ruby program, we need to run the Ruby command in the terminal or a console. 0:56 So click down here in the Console area, and 1:01 then type ruby, a space, and the name of the file you want to run. 1:04 So, that's widgets.rb. 1:08 As long as you're in the same folder, or 1:12 directory as that file, then all you need to type is the file name. 1:14 And you can see that down here, the program runs and 1:18 it prints our message, hello world. 1:21 If you're in a different folder or directory than the file you want to run, 1:23 then you're going to need to change into that directory before running the file. 1:27 So let's try creating a new folder here, we'll call it my_folder. 1:31 And then we'll create a new file within it. 1:37 And we'll call it other.rb. 1:40 And we'll just do another simple program in here that prints out other file. 1:43 Go to the File menu and choose Save. 1:50 And by the way a shortcut, if you want is you can hold down the Cmd key on MacOS, 1:53 or the Ctrl key on Windows and press S to save the file. 1:59 We'll be doing that a lot for the rest of the course. 2:03 So now we have this file named other.rb saved in a folder named my_folder. 2:06 If I try going down here clicking in the Console and running ruby other.rb, 2:11 it won't work, because there's no file named other.rb in the current directory. 2:16 Instead, we need to change directories using the cd or change directory command. 2:22 And we need to type in the name of the directory or 2:28 folder that we want to change into. 2:31 So we'll change into the folder named my folder. 2:33 And now that we're in this other directory, we can go ahead and 2:36 run the other.rb program and Ruby will find it successfully. 2:41 And there you see it prints our message, other file. 2:46 But we don't need to do any of that right now, 2:49 we're okay working in just the one folder. 2:51 So I'm going to change back to the parent directory using this special notation 2:53 here, .., which means the parent of the current directory that 2:58 is the directory that holds the current directory. 3:01 And I'll hit Enter, so that I'm back in the workspace folder where I began. 3:04 And now I'm going to delete my_folder. 3:09 And that I'll delete the other.rb file along with it. 3:13 Now let's take a closer look at the code within our widgets.rb file. 3:16 Puts is a method, 3:22 a group of code statements that together perform a particular task. 3:23 Puts stands for put string, and that's the task it carries out. 3:27 It puts a string of text characters on the terminal output. 3:31 Typing the method name here calls that method or causes it to run. 3:35 The text here inside quotes is a string. 3:39 We'll be talking more about string soon. 3:41 Because we are putting the string here following the method call, 3:44 it's passed as an argument to the method. 3:47 Methods take the arguments that are provided when they're called and 3:49 work with them in some way. 3:52 Puts just prints whatever arguments it receives out to the terminal. 3:54 Let's add a few different method calls here so 3:58 you can see what I'm talking about. 4:00 First, we'll make another call to puts, and this time, 4:02 we're going to type parenthesis following the method name. 4:06 We'll pass at several arguments. 4:09 If you're passing multiple arguments to a method, you separate them with commas. 4:12 So we'll pass a couple of numbers here as arguments, and 4:16 then we'll pass a couple of strings too. 4:18 And then we type a closing parenthesis to end the method call. 4:22 Now let's make another call to a different method, one called sleep. 4:26 What sleep does is it causes the program to pause execution for 4:31 the number of seconds you specify. 4:34 So you pass an argument with number of seconds that you 4:37 want the program to sleep for. 4:40 Now let's add another method call. 4:42 This one to the print method. 4:43 Print works a lot like puts except that while puts skips to a new line 4:46 after each new argument that it receives, print just stays on the same line. 4:51 So we'll pass a couple more string arguments to print. 4:55 Let's say, c and d. 4:58 And then let's make one more call to sleep, and we'll just have it sleep for 5:01 one second this time. 5:05 We'll hit Cmd+S to save this, Ctrl+S if you're on a Windows machine. 5:06 And now let's click down here on the Console, and run the program again. 5:10 We'll hit the up arrow to bring the previous command back and 5:14 run ruby widgets.rb. 5:17 And you see that as before, 5:22 it makes a call to puts with the string hello world and it prints that out. 5:23 And then comes our call to puts with the numbers 1 and 2, and 5:27 the low strings a and b. 5:31 And you can see that each of those prints on a separate line down here. 5:34 If we rerun the program again, you'll notice that after it prints one 1, 2 a, 5:38 b, it pauses for a couple seconds before printing c and d here on the same line. 5:42 That pause was due to the call to sleep with an argument of 2, 5:47 it slept for 2 seconds. 5:52 Then the call to print printed the string c and d on the same line. 5:54 And finally, the program slept for one more second before ending. 5:59 Parentheses are optional on Ruby method calls. 6:02 So, you'll notice that we made a call to puts up here without any parentheses 6:05 surrounding it. 6:10 We just put a space between the method name and its one argument. 6:10 We can actually remove parentheses from all of these method it calls down here, 6:14 and they'll still run in the same way. 6:20 So I'm going to replace the opening parenthesis each with a space, and 6:22 I will just delete the closing parenthesis on each line. 6:26 Hit Cmd+S to save that, Ctrl+S if you are on Windows. 6:29 And let's rerun the program again and 6:33 you will see that it runs in just the same way. 6:35
You need to sign up for Treehouse in order to download course files.Sign up