Defining Methods4:00 with Jay McGavren
We've shown you how to call methods that Ruby defines for you. Now let's learn how to define your own methods.
- Defining a method
- method name
- method body: one or more lines of code that will be run when method is called
- lines of method body are usually indented to make it clear they're a part of the method, although this isn't required
def wait puts "Waiting..." sleep 3 puts "Done" end def count_to_three puts 1 puts 2 puts 3 end
- Valid method names
- All lower case
- Numbers are legal but rarely used
- Separate words with underscores. This is called snake case because it makes the name look like it's crawling on the ground.
- Call a method by typing its name in your code
- Can call a method as many times as we want
Here I've created another file named temp.rb that contains some new Ruby code. 0:00 Lines 1, 2, and 3 display a message that we're waiting, pause for 0:04 3 seconds, and then print another message that we're done waiting. 0:08 Lines 4, 5, and 6 count from 1 to 3. 0:13 These are two separate tasks in the same program, but it's hard to tell at a glance 0:17 which lines belong to which task, or what task they're supposed to be doing. 0:21 It's easy to define your own methods in Ruby. 0:26 You start with the def keyword, short for 0:28 define, followed by the name of the method you want. 0:31 So let's create a new method here named wait. 0:34 And next we're going to need a method body, that's one or 0:37 more lines of code that will be run when the method is called. 0:39 The lines of a method body are usually indented to make it clear that they're 0:43 part of the method, although this isn't required by the Ruby interpreter itself. 0:47 But it's a very common standard practice. 0:51 I would definitely recommend indenting your method lines. 0:53 So we're just going to take these first three lines of existing code and 0:56 put them inside a wait method. 1:00 The end of the method is marked by the end keyword. 1:02 And the end keyword should be aligned with the def keyword at the start of 1:07 the method. 1:10 So we'll convert those first three lines of code to a wait method. 1:11 And now let's define a count_to_three method to hold the remaining code. 1:15 We'll just take those existing lines of code and 1:25 indent them to form the body of the second method. 1:27 And again, we'll end the method with the end keyword. 1:31 You don't get to use just any character you want in a method name. 1:34 Generally speaking, method names should be all lowercase. 1:38 You can add numbers into them, but those are rarely used, so try to avoid that. 1:41 If there are multiple words in your method name, 1:45 you should separate them with underscore characters. 1:47 This style is called snake case, 1:51 because it makes the name look like it's crawling on the ground. 1:53 Now that we've defined the methods, we need to call them so 1:56 that they're actually executed. 1:58 We do that just like we did the predefined methods, 2:00 we simply type the name of the method to call it. 2:03 So count_to_three will call the count_to_three method that we've defined, 2:05 and wait will call the wait method that we've defined. 2:11 Let's hit Command + S to save that, Ctrl + S if you're on Windows. 2:16 And click down in the console area to try running it again. 2:21 Up arrow to bring up the previous command. 2:25 And you can see that our call to count_to_three causes the count_to_three 2:29 method to run and print the numbers 1, 2 and 3. 2:33 And then the call to wait causes the wait method to run and print the message, 2:36 waiting, then sleep for 3 seconds, and then print the message, done. 2:43 We can call a method during a program as many times as we want. 2:47 So let's add a couple additional calls to count_to_three down here. 2:50 I'll just copy the first call and paste it in a couple more times, save my work. 2:54 And re-run it from the console. 3:01 And you can see that it calls count_to_three once. 3:03 It calls wait once and then it calls count_to_three two more times down here. 3:07 Now that we understand method calls a little bit better, 3:13 let's go back to our widgets.rb file by clicking on it here on the side bar. 3:15 And let's implement our welcome message to the user. 3:21 We can do that with the single call to the puts method. 3:23 We'll pass it a string with the message, Welcome to the widget store!, 3:26 close the string with a pair of double quotes and save that. 3:33 And then click down here in the console to run it. 3:39 We do that with ruby widgets.rb. 3:42 And you can see that it prints our welcome to the widget store message. 3:48 Our program is displaying a welcome message to the user. 3:53 We can cross the first requirement off our list. 3:56
You need to sign up for Treehouse in order to download course files.Sign up