Locked lesson.
About this lesson
Understanding methods in Ruby.
Exercise files
Download this lesson’s related exercise files.
Methods.docx60.4 KB Methods - Solution.docx
59.7 KB
Quick reference
Methods
Methods are like programs inside of your programs that only get executed when you call them.
When to use
Use methods whenever you can!
Instructions
To create a method:
def method_name
# your code here
end
To call a method:
method_name
To create a method that accepts parameters from outside itself:
def method_name(parameter)
# your code here
end
And to pass in a paramter when calling the method:
method_name(parameter)
Hints & tips
- Methods are programs inside your program
- Methods only get executed if you call them
- To call a methods, just type its name
- 00:04 We're starting to get into more advanced topics, and
- 00:07 in this video I want to talk about methods.
- 00:09 And methods are very important for Ruby and any programming language.
- 00:13 And basically, a method is a block of code that
- 00:16 doesn't get executed when your program runs until you tell it to.
- 00:21 So it's a good way to keep chunks of code separate,
- 00:23 and only call them when you need them.
- 00:26 That's a good way to keep your code clean and clear, and
- 00:28 it's just a good programming thing that you're going to do.
- 00:30 So to create a method in Ruby, it's really simple.
- 00:33 You just define, you type def to define it, and then you name it.
- 00:38 So let's call this name.
- 00:40 And then you put these parentheses guys here, and
- 00:43 that's how we pass arguments into our method.
- 00:45 I'll show you about that in just a minute.
- 00:47 So below that we just type end, and now we've created a method.
- 00:50 It doesn't do anything, but it exists.
- 00:53 So inside of here we can write whatever code we want, right?
- 00:56 So we've seen this sort of general format before with our if statements, our loops.
- 01:00 You do something, you end something, in the middle is where the stuff goes.
- 01:03 So here I can just puts "Hello Bob".
- 01:07 So we save this.
- 01:08 Now, if I run this, nothing happened.
- 01:12 Because like I said, methods don't get run automatically when your program runs.
- 01:16 They only work, they only get called, if you call them, if you invoke them.
- 01:20 In order to invoke a method, you type its name.
- 01:24 In this case, it's name.
- 01:25 Now, we don't have to type puts, we don't have to do anything,
- 01:27 we just invoke the name of the method.
- 01:29 So now if we run it it says, Hello Bob.
- 01:31 So this is pretty interesting.
- 01:33 We can pass things into our method, right?
- 01:36 Let's change this guy to namer, we'll call him namer.
- 01:39 So let's say we want to pass a first_name.
- 01:44 And you don't have to put these spaces,
- 01:46 I just do it because I like the way it looks.
- 01:48 And let's say we want to output Hello.
- 01:52 And let's use our interpolation, with our little brackets, and go #{first_name}
- 01:57 So now, if I run this I'm going to get an error.
- 02:00 Watch, I got an error, because when I called it I didn't pass any arguments in,
- 02:05 and our program is looking for an argument, this first_name,
- 02:09 to be passed in.
- 02:10 So to pass in arguments to a method, you just type in those parentheses and
- 02:14 put whatever you want in there.
- 02:15 So let's say, John.
- 02:17 So now we're passing John into our method, and first_name becomes John.
- 02:23 It's almost like we went first_name = John, right?
- 02:28 But we don't have to explicitly do that, this thing right here does it for us.
- 02:33 So if we save this, now if we run it, it says Hello John.
- 02:37 If we change this to Tim, it doesn't matter what we change it to,
- 02:43 it gets passed into our method.
- 02:46 And then the code is executed, and it does whatever we've told it to do.
- 02:49 So in a very, very simplistic way, those are methods.
- 02:53 They're very important, they're very powerful.
- 02:55 We're going to use them forever with your programming,
- 02:57 it's just one of those things.
- 02:58 And in the next video, I'm going to talk a little bit more about methods.
- 03:01 We could talk for days about methods, but in the next video,
- 03:04 we're going to finish up methods and talk a little bit more in detail about them.
Lesson notes are only available for subscribers.