Locked lesson.
About this lesson
More on methods...
Exercise files
Download this lesson’s related exercise files.
Methods Part 2.docx60.3 KB Methods Part 2 - Solution.docx
58 KB
Quick reference
Methods Part 2
Let's look at methods in more depth.
When to use
Methods are important for most Ruby programming.
Instructions
Don't call a method before you define it, or you'll get an error.
You can pass as many parameters into a method as you like, as long as the method expects those parameters.
You can call a method from inside another method.
Try not to use puts in a method, instead use return and then "puts out" your method.
Hints & tips
- Be sure to only call your method after you define it!
- 00:04 In the last video, we started talking about methods.
- 00:06 In this video, I want to continue on and flesh it out a little bit more.
- 00:09 So the first thing I want to talk about is these parameters.
- 00:12 We passed one parameter, but you could easily do more than one,
- 00:16 as many as you want.
- 00:16 You just have to come down here, and whenever you invoke your method,
- 00:21 just be sure to pass in the correct number of parameters.
- 00:25 Now our program here, our method is looking for two parameters,
- 00:29 two things to be passed in.
- 00:30 So we have to be sure to pass in two things.
- 00:33 So now we can add this last name in just by obviously sticking in like this.
- 00:39 Now if we save and run it, we get a Hello: John, Elder.
- 00:43 So that's the first thing I want to mention.
- 00:45 You can pass as many parameters and as you want,
- 00:46 once you get them into your program, they're assigned into these variables.
- 00:51 You can use these variables in any way.
- 00:52 You can do any kind of code inside your method.
- 00:54 You could do loops, you could do if statements,
- 00:57 else statements, anything you want inside your method.
- 01:00 So now I want to talk about the scope and the flow of our program.
- 01:03 Ruby programs, they start at the top, and they move down.
- 01:06 And every line, they move down, they execute whatever's in that line, and
- 01:10 then Ruby moves to the next line.
- 01:12 Well, methods don't get run until you specifically call them, right?
- 01:16 So the flow of the program starts at the top, it goes down, it sees this,
- 01:20 it reads it, it puts it into memory.
- 01:23 But it doesn't do anything until down here when you call it.
- 01:26 You have to be very careful about that.
- 01:28 If I copy this and deleted it, and put this up here, we're going to get an error.
- 01:32 So let's save this and run it, and we see undefined method.
- 01:36 So what's going on here is the program is starting at the top,
- 01:39 it clears the screen, it comes down here it says call namer.
- 01:42 Well, the program is only gotten to line 3, and
- 01:45 on line 2 there was nothing, and on line 1 it was just this clear thing.
- 01:49 So our program has no earthly idea what name or
- 01:52 is, it hasn't gotten down to it yet.
- 01:54 So you get an error, it says undefined method.
- 01:56 It says, I don't know what namer is, I can't run this.
- 01:59 Until it gets down here and puts this into memory, you can't use it.
- 02:03 So whenever you're using methods, it's always, always, always a good idea to put
- 02:08 all of your methods at the very top of your program, all of them, right?
- 02:13 So then, as soon as the program starts, it sees the method, it puts it into memory.
- 02:19 And now from now on whenever you want, you can call this method just by calling it,
- 02:23 and you won't get any errors.
- 02:24 So that's very, very important.
- 02:26 Another thing is you can call define, let's call this mather.
- 02:32 No parameters, an inside here, we're just going to go, puts 2 + 2, right?
- 02:38 So if I save this, bring this guy up to the top here.
- 02:43 Always put our methods at the top.
- 02:44 Now inside this method I could call mather, right?
- 02:48 What happens now?
- 02:49 We get Hello: John Elder and then 4, because mather does 2 + 2, and returns 4,
- 02:54 and so it outputs it right there.
- 02:55 So you can call other methods from inside your method, but
- 02:59 again remember the flow of your program.
- 03:02 So if we had mather, our method down here, and
- 03:08 if we called it right here, we're probably going to get an error, right?
- 03:14 Let's run this and see.
- 03:15 Yeah, we get an error.
- 03:15 So why?
- 03:16 Well, program starts at the beginning it moves down, it sees this namer.
- 03:20 It says, okay, I can run that, it puts it into memory,
- 03:23 it sees this mather it doesn't know what it is, but
- 03:25 it doesn't really care because we're not running this yet.
- 03:28 And it gets down to here, and we're calling namer.
- 03:30 So our program goes I remember namer it's in my memory it runs it but
- 03:34 then it gets to this thing, and it goes, what is this mather?
- 03:37 I don't know, because the program hasn't gotten down past line 10 yet, and
- 03:42 mather is down here, and so it can't find it and it gets an error.
- 03:45 So program flow is important, best thing is just a put all of your methods always
- 03:50 at the top of your programs, and then you wont have this problems.
- 03:53 But those are methods, we're going to use them a lot,
- 03:55 you're going to use them forever, they are just very, very important.
- 03:58 One more thing, puts is not a great thing to do inside of a method for
- 04:02 a lot of different reasons.
- 04:03 But more so because you lose a lot of the control outside of your method.
- 04:07 So let me just show you really quickly, we get rid of this reference to mather.
- 04:10 And let's just get rid of our mather method completely, and instead of puts,
- 04:14 let's do return.
- 04:16 Now if we save this and
- 04:17 run it, nothing happens because all we're doing here is returning it.
- 04:21 And right here we're calling the method, it returns it, but
- 04:24 we're not doing anything with that return thing.
- 04:27 So outside of our method we could decide what we want to do with it.
- 04:31 So if we want to puts that to the screen we can do that, we run it,
- 04:34 Hello: John Elder.
- 04:35 If we want to assign this to a variable we could say a =, and
- 04:38 then of course we run this nothing's going to happen again,
- 04:41 because we haven't done anything with this variable.
- 04:43 But now we can do something with this variable, we can puts it hello:
- 04:48 John Elder, we could change it you know to .downcase, now everything is lowercase.
- 04:55 So it gives us a flexibility when we return our methods stuff instead of just
- 04:58 puts-ing it to the screen, in which case, it's just going to puts it to the screen.
- 05:03 And there's nothing we can do about it.
- 05:04 So most of the time when you deal with methods,
- 05:06 you're going to want to use something like return.
- 05:08 So those are methods.
- 05:09 In the next video, we're going to talk about random numbers.
Lesson notes are only available for subscribers.