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