Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:06,720
Now, so far in the course we've been using built-in Ruby methods.
2
00:00:06,720 --> 00:00:10,680
And they've been really handy in the last exercise you used them to print out your players
3
00:00:10,680 --> 00:00:12,360
in four different formats.
4
00:00:12,360 --> 00:00:16,400
And that's been fun and all, but it's probably time to decide on a single way to start the
5
00:00:16,400 --> 00:00:18,400
game for all our players.
6
00:00:18,400 --> 00:00:21,280
In other words, we need some consistency here.
7
00:00:21,280 --> 00:00:23,560
We actually have another issue to address here as well.
8
00:00:23,560 --> 00:00:28,080
When you say we have four players in four different formats, we probably have four different
9
00:00:28,080 --> 00:00:31,360
pieces of code, which means we've got duplication in our code.
10
00:00:31,360 --> 00:00:35,440
Notice that we have four stanzas of code, each printing a player in a slightly different
11
00:00:35,440 --> 00:00:36,440
format.
12
00:00:36,440 --> 00:00:39,960
Now, to make these consistent, we might be inclined to change each stanza.
13
00:00:39,960 --> 00:00:42,080
But what happens when the fifth player comes along?
14
00:00:42,080 --> 00:00:44,440
Or what happens when we want to change the format again?
15
00:00:44,440 --> 00:00:46,500
Well, we're going to end up with a real mess.
16
00:00:46,500 --> 00:00:51,040
And every time we want to make a change, we'll have to make it in multiple places.
17
00:00:51,040 --> 00:00:54,599
One of the basic tenets of good programming is to avoid duplication.
18
00:00:54,599 --> 00:00:57,200
This is sometimes called the dry principle.
19
00:00:57,200 --> 00:00:58,840
But repeat yourself.
20
00:00:58,840 --> 00:01:00,520
So how do we dry up code like this?
21
00:01:00,520 --> 00:01:05,460
Well, we want to be able to put all the formatting code in one place and then call that code
22
00:01:05,460 --> 00:01:07,400
any time we want to print a player.
23
00:01:07,400 --> 00:01:09,880
To do that, we need to write a method.
24
00:01:09,880 --> 00:01:13,580
So in the exercise, we're going to give you a chance to clean up the inconsistency and
25
00:01:13,580 --> 00:01:15,380
duplication in the game.
26
00:01:15,380 --> 00:01:19,000
And to show you how to do that, we're going to look at a similar situation we have with
27
00:01:19,000 --> 00:01:20,720
our movie app.
28
00:01:20,720 --> 00:01:23,960
While you were working on the last exercise, we added some more movies.
29
00:01:23,960 --> 00:01:25,760
Here's where our code stands now.
30
00:01:25,760 --> 00:01:30,160
Right, so we've made the formatting consistent here, but we still have duplication.
31
00:01:30,160 --> 00:01:32,400
Yeah, we have some duplication in our code.
32
00:01:32,400 --> 00:01:33,680
We'd like to clean all this up.
33
00:01:33,680 --> 00:01:37,720
So we're going to write a method to clean it up and put it all in one spot.
34
00:01:37,720 --> 00:01:41,640
OK, so I'm just going to delete all of this code because there's not that much here.
35
00:01:41,640 --> 00:01:44,760
And we're going to remove all this duplication anyway.
36
00:01:44,760 --> 00:01:47,340
And I'm just going to start with a really simple method.
37
00:01:47,340 --> 00:01:50,760
Method starts with the keyword def, and then we give the method name.
38
00:01:50,760 --> 00:01:54,160
We want to print a movie listing, so let's just call this movie listing.
39
00:01:54,160 --> 00:01:56,740
And then it ends with the keyword end.
40
00:01:56,740 --> 00:01:59,119
And then inside of that method, what we want to do is print out a movie.
41
00:01:59,119 --> 00:02:01,560
So I'm going to use our old friend putS.
42
00:02:01,560 --> 00:02:06,080
I'm going to print out movie, movie, and we're just going to stick with Goonies for right
43
00:02:06,080 --> 00:02:07,080
now.
44
00:02:07,080 --> 00:02:08,840
Now defining the method doesn't do anything.
45
00:02:08,840 --> 00:02:12,040
If we try to run this, we don't get any output here.
46
00:02:12,040 --> 00:02:15,120
To call the method, we just call it by its name.
47
00:02:15,120 --> 00:02:16,880
We just call movie listing like that.
48
00:02:16,880 --> 00:02:18,359
Remember, we have an object here.
49
00:02:18,359 --> 00:02:22,980
It's this object that's stored in self, so there is a receiver for this method.
50
00:02:22,980 --> 00:02:26,239
And in a top level program like this, we've seen how that's already arranged.
51
00:02:26,239 --> 00:02:30,079
I can just call it now, and sure enough, we get the movie listing.
52
00:02:30,079 --> 00:02:33,480
Of course, now that we have a method, we could call it any number of times.
53
00:02:33,480 --> 00:02:36,940
So we might want to print this movie listing out, say, three times.
54
00:02:36,940 --> 00:02:38,079
And there we go.
55
00:02:38,079 --> 00:02:43,399
This code actually contains two method calls, one on the method movie listing and the other
56
00:02:43,399 --> 00:02:44,720
on the method putS.
57
00:02:44,720 --> 00:02:45,720
Right.
58
00:02:45,720 --> 00:02:46,720
So we've got a nested method call.
59
00:02:46,720 --> 00:02:50,040
We call the movie listing method, which goes into this method, and then it turns around
60
00:02:50,040 --> 00:02:51,359
and calls putS there.
61
00:02:51,359 --> 00:02:52,359
So we're actually calling two methods.
62
00:02:52,360 --> 00:02:53,440
You're right.
63
00:02:53,440 --> 00:02:57,200
So right now, every time we call the method, it prints the movie listing to the screen.
64
00:02:57,200 --> 00:02:58,200
Right.
65
00:02:58,200 --> 00:02:59,200
And what if we don't want that every time?
66
00:02:59,200 --> 00:03:03,280
What if we want instead it to return a string, then we can evaluate it or do something with
67
00:03:03,280 --> 00:03:04,280
it?
68
00:03:04,280 --> 00:03:06,600
So let's change the method to actually return a string.
69
00:03:06,600 --> 00:03:07,600
Yeah.
70
00:03:07,600 --> 00:03:10,280
So let's just remove this putS from inside of the method.
71
00:03:10,280 --> 00:03:15,680
Now if we run it, well, we get no output because we're calling the method, and it's just returning
72
00:03:15,680 --> 00:03:17,780
a string, but we're not doing anything with it.
73
00:03:17,780 --> 00:03:21,160
So let's say in the first case, we actually do want to print out that movie listing to
74
00:03:21,160 --> 00:03:22,160
the console.
75
00:03:22,160 --> 00:03:24,040
So we put a putS there.
76
00:03:24,040 --> 00:03:25,720
We're going to call the movie listing method.
77
00:03:25,720 --> 00:03:28,880
It's going to return a string and putS takes a string.
78
00:03:28,880 --> 00:03:32,079
So if we run it now, we should get one movie listing.
79
00:03:32,079 --> 00:03:36,120
Now you may have noticed in Ruby, we didn't have to explicitly return something inside
80
00:03:36,120 --> 00:03:37,180
of that method.
81
00:03:37,180 --> 00:03:42,320
What happens is the last expression that's evaluated in the method, the result of that
82
00:03:42,320 --> 00:03:44,920
expression is then returned from the method.
83
00:03:44,920 --> 00:03:48,400
And just to demonstrate this, we've only got one line in this method right now.
84
00:03:48,400 --> 00:03:52,640
If I were to change this to have, oh, I don't know, something like the number 10 as the
85
00:03:52,640 --> 00:03:57,840
last expression then, if I run it now, we actually get 10 printed out to the console
86
00:03:57,840 --> 00:04:01,560
because it's the last expression in the method.
87
00:04:01,560 --> 00:04:05,440
Now it would be nice if this method were more generic and we could print like any movie.
88
00:04:05,440 --> 00:04:06,440
Sure.
89
00:04:06,440 --> 00:04:08,200
Let's pass in that movie name as a parameter.
90
00:04:08,200 --> 00:04:12,740
So at the end of the method definition here, we just pass in a parameter in parentheses
91
00:04:12,740 --> 00:04:14,320
and we'll call the parameter title.
92
00:04:14,320 --> 00:04:17,000
That's going to hold on to the name of the movie we pass in.
93
00:04:17,000 --> 00:04:20,560
And then we've already seen we can substitute in variables to strings.
94
00:04:20,560 --> 00:04:23,340
So let's use the interpolation syntax here again.
95
00:04:23,340 --> 00:04:24,520
We use title.
96
00:04:24,520 --> 00:04:28,040
And just to be a little bit flash, let's just go ahead and capitalize that title name because
97
00:04:28,040 --> 00:04:30,300
we know we can call methods inside of there as well.
98
00:04:30,300 --> 00:04:32,980
Now if we run the program now, we're going to get an error.
99
00:04:32,980 --> 00:04:36,640
It says the wrong number of arguments, 0, 4, 1.
100
00:04:36,640 --> 00:04:39,820
So we need to pass in our actual movie name.
101
00:04:39,820 --> 00:04:41,440
I'm going to drop these two other listings.
102
00:04:41,440 --> 00:04:43,420
We don't actually need those right now.
103
00:04:43,420 --> 00:04:46,560
So to get the same string we had before, let's pass in Goonies.
104
00:04:46,560 --> 00:04:50,480
We can pass it in lowercase because it's going to get capitalized.
105
00:04:50,480 --> 00:04:53,180
And we're right back to our movie listing.
106
00:04:53,180 --> 00:04:56,760
But the cool thing about this now is we can print out different movies, which was kind
107
00:04:56,760 --> 00:04:57,760
of the goal.
108
00:04:57,760 --> 00:05:01,520
So we can have Ghostbusters.
109
00:05:01,520 --> 00:05:04,320
Now we could also store the movie name in a local variable.
110
00:05:04,320 --> 00:05:07,440
Let's say I had a local variable called aTitle.
111
00:05:07,440 --> 00:05:10,120
And we're just going to set that to Goldfinger.
112
00:05:10,120 --> 00:05:13,420
And then I could call putAs movie listing.
113
00:05:13,420 --> 00:05:16,420
And then I could just pass in that variable, aTitle.
114
00:05:16,420 --> 00:05:21,180
So this local variable name does not have to match this parameter name.
115
00:05:21,180 --> 00:05:24,800
What happens is this local variable ends up pointing to the string Goldfinger.
116
00:05:24,800 --> 00:05:27,920
And this title parameter also ends up pointing to Goldfinger.
117
00:05:27,920 --> 00:05:31,900
So you can either do them inline, as we did above, or you can have local variables and
118
00:05:31,900 --> 00:05:34,440
then pass those in as the parameters.
119
00:05:34,440 --> 00:05:36,400
How about printing the movie's rank too?
120
00:05:36,400 --> 00:05:38,160
Yeah, we could make that generic too.
121
00:05:38,160 --> 00:05:41,000
We'll just pass it in as another parameter to the movie listing.
122
00:05:41,000 --> 00:05:43,540
I'll just use a comma to separate the parameters.
123
00:05:43,540 --> 00:05:45,040
And we'll just use rank.
124
00:05:45,040 --> 00:05:51,020
And then inside of our string, we'll say has a rank of rank, just like that.
125
00:05:51,020 --> 00:05:54,600
And then when we call the method, when we're printing out Goonies, we have to pass in a
126
00:05:54,600 --> 00:05:56,440
value for that to fill in that variable.
127
00:05:56,440 --> 00:06:02,340
So let's say Goonies has a rank of 10 and Ghostbusters has a rank of 9, for example.
128
00:06:02,340 --> 00:06:05,200
So if we run it now, we see we have two movie listings.
129
00:06:05,200 --> 00:06:09,560
We've got Goonies has a rank of 10, Ghostbusters has a rank of 9, but it's not printing out
130
00:06:09,560 --> 00:06:10,560
Goldfinger.
131
00:06:10,560 --> 00:06:12,780
We get this error wrong number of arguments.
132
00:06:12,780 --> 00:06:15,159
And that's because we're not passing in rank.
133
00:06:15,159 --> 00:06:17,679
And right now, rank is a required parameter.
134
00:06:17,679 --> 00:06:20,159
So what if we didn't want to pass in a rank for Goldfinger?
135
00:06:20,159 --> 00:06:21,479
Well let's fix that.
136
00:06:21,479 --> 00:06:23,900
So method parameters in Ruby can have default values.
137
00:06:23,900 --> 00:06:27,900
And we do that, we just come up to the parameter in the method definition and then we use the
138
00:06:27,900 --> 00:06:29,900
equal sign and then we give it the default value.
139
00:06:29,900 --> 00:06:33,500
So let's say the default rank for movies is going to be 0.
140
00:06:33,500 --> 00:06:35,559
Now if we do that, we don't have to pass in a rank.
141
00:06:35,559 --> 00:06:40,119
If we run the program again, now we have our Goldfinger movie and it has a default rank
142
00:06:40,119 --> 00:06:41,760
of 0.
143
00:06:41,760 --> 00:06:46,400
So something that's kind of cool is these default parameter values can be derived from
144
00:06:46,400 --> 00:06:47,640
previous parameter values.
145
00:06:47,640 --> 00:06:52,120
We could say something like the rank is equal to the length of the title.
146
00:06:52,120 --> 00:06:55,440
The title is a parameter here and we're just going to derive the rank from that.
147
00:06:55,440 --> 00:07:00,460
So if we run it now, Goldfinger has a rank of 10 because Goldfinger has 10 characters
148
00:07:00,460 --> 00:07:01,460
inside of it.
149
00:07:01,460 --> 00:07:05,000
And we know that if you have a longer title, you have a higher ranked movie.
150
00:07:05,000 --> 00:07:06,000
Yeah, that's true.
151
00:07:06,000 --> 00:07:07,620
You get a better rank for the longer title.
152
00:07:07,620 --> 00:07:12,760
So I'm just going to set this back to 0 just so we have that set back there.
153
00:07:12,760 --> 00:07:16,400
And I'm going to also drop this movie part of the string because we kind of know that
154
00:07:16,400 --> 00:07:18,040
we're printing a movie listing.
155
00:07:18,040 --> 00:07:20,340
And there we've got our three movies.
156
00:07:20,340 --> 00:07:23,040
So we've seen four different method styles here.
157
00:07:23,040 --> 00:07:25,020
Let's recap the syntax.
158
00:07:25,020 --> 00:07:27,820
So here's a simple method that doesn't take any parameters.
159
00:07:27,820 --> 00:07:30,620
To run the method, we call it by name.
160
00:07:30,620 --> 00:07:32,680
But what if we want to pass in a person's name?
161
00:07:32,680 --> 00:07:37,120
Well, we declare that the method takes one parameter and call the method with the person's
162
00:07:37,120 --> 00:07:38,240
name.
163
00:07:38,240 --> 00:07:41,760
We could also assign a default person's name if none is given.
164
00:07:41,760 --> 00:07:45,280
If we don't pass it a name, it prints, howdy, partner.
165
00:07:45,280 --> 00:07:49,080
And then to override the default, we pass in a specific name.
166
00:07:49,080 --> 00:07:54,380
Finally, we could declare that a method takes two parameters, say, name and age.
167
00:07:54,380 --> 00:07:56,360
In this case, both are required.
168
00:07:56,360 --> 00:07:59,520
But we could assign defaults here as well.
169
00:07:59,520 --> 00:08:01,400
So think of a method like a little black box.
170
00:08:01,400 --> 00:08:05,260
It takes some parameters, it does something with them, and then it returns some result.
171
00:08:05,260 --> 00:08:07,659
Now right now, our method simply returns a string.
172
00:08:07,659 --> 00:08:09,380
So let's add a bit more to it.
173
00:08:09,380 --> 00:08:13,680
So let's say we wanted to print the weekday as part of our movie listing here.
174
00:08:13,680 --> 00:08:18,039
So I'm going to define a couple local variables inside of this method to set that up.
175
00:08:18,039 --> 00:08:19,440
So first, I just want the current time.
176
00:08:19,440 --> 00:08:23,719
I'm going to assign that to a local variable current time, like that.
177
00:08:23,719 --> 00:08:25,500
And then I want the weekday part of that.
178
00:08:25,500 --> 00:08:28,440
So I want, like, Monday, Tuesday, Wednesday, and so on.
179
00:08:28,440 --> 00:08:30,780
So I'm going to define that in a variable called today.
180
00:08:30,780 --> 00:08:32,539
I'm going to use current time.
181
00:08:32,539 --> 00:08:37,860
And then I'm going to call this method strf time and pass it in a format, %a.
182
00:08:37,860 --> 00:08:41,280
And all that's going to do is it's going to format the time in a weekday format.
183
00:08:41,280 --> 00:08:43,919
So I'm going to have, like, Monday, for example.
184
00:08:43,919 --> 00:08:46,740
Then inside of the string, I'll go ahead and use that variable.
185
00:08:46,740 --> 00:08:53,020
It has a rank of whatever, and I'll say as of today is the variable name.
186
00:08:53,020 --> 00:08:57,240
So now if I run this, we've got our movie listing, and it's Thursday, so all these movie
187
00:08:57,240 --> 00:08:59,459
listings are good as of today.
188
00:08:59,460 --> 00:09:03,500
So today is a local variable inside of this method, and we said that methods are like
189
00:09:03,500 --> 00:09:04,620
little black boxes.
190
00:09:04,620 --> 00:09:10,100
So if we were to try to access that variable today outside of the method, well, we get
191
00:09:10,100 --> 00:09:16,140
this error, undefined local variable or method today, because today isn't in scope at this
192
00:09:16,140 --> 00:09:17,280
level of the program.
193
00:09:17,280 --> 00:09:20,660
It's only visible or accessible inside of that method.
194
00:09:20,660 --> 00:09:23,220
The same thing is true for current time, for example.
195
00:09:23,220 --> 00:09:25,380
You get the exact same error.
196
00:09:25,380 --> 00:09:29,740
And in fact, if we were to try to print out title, which is a parameter of this method
197
00:09:29,740 --> 00:09:36,060
right here, we run that, we get the same thing, undefined local variable or method title.
198
00:09:36,060 --> 00:09:41,360
So even the parameters or the parameter names, title and rank in this case, aren't accessible
199
00:09:41,360 --> 00:09:42,980
outside of that method.
200
00:09:42,980 --> 00:09:46,540
So again, a method is like a little black box.
201
00:09:46,540 --> 00:09:48,620
So Mike, we have a single method here.
202
00:09:48,620 --> 00:09:52,480
We should show them that methods can call other methods, because all good programs are
203
00:09:52,480 --> 00:09:56,020
composed of many small methods that just do one thing.
204
00:09:56,020 --> 00:10:00,480
So maybe it would be handy if we had a method, maybe call it weekday, that just returns the
205
00:10:00,480 --> 00:10:02,940
weekday name, and we can pull it out of this method.
206
00:10:02,940 --> 00:10:04,420
Yeah, that's a good idea.
207
00:10:04,420 --> 00:10:08,940
And then the movie listing could all, could then turn around and call that weekday method.
208
00:10:08,940 --> 00:10:12,540
And in fact, we've got a couple things going on inside of this movie listing.
209
00:10:12,540 --> 00:10:16,980
We're trying to get the weekday, which we do in the first couple lines, and then we
210
00:10:16,980 --> 00:10:18,340
actually print out the movie listing.
211
00:10:18,340 --> 00:10:20,860
So it's almost doing too much as it is.
212
00:10:20,860 --> 00:10:22,220
So let's take this out.
213
00:10:22,220 --> 00:10:24,180
I'm actually going to cut out this code.
214
00:10:24,180 --> 00:10:29,020
And then above this method, I'll define a method called, oh, you had the name weekday,
215
00:10:29,020 --> 00:10:30,180
which works really well.
216
00:10:30,180 --> 00:10:32,820
And then I'll just paste that code in there.
217
00:10:32,820 --> 00:10:36,740
So all this weekday method does, it gets the current time, and then it formats it in the
218
00:10:36,740 --> 00:10:37,740
weekday format.
219
00:10:37,740 --> 00:10:41,420
And in fact, we don't even need this local variable, because we know that the last expression
220
00:10:41,420 --> 00:10:44,300
evaluated method is going to be automatically returned.
221
00:10:44,300 --> 00:10:46,700
So we're going to return the weekday here.
222
00:10:46,700 --> 00:10:51,640
Then inside of this method, we could go back to assigning, today will just be whatever
223
00:10:51,640 --> 00:10:54,580
the weekday method returns.
224
00:10:54,580 --> 00:10:57,300
I've got to remove this, or we're going to get this error down here.
225
00:10:57,300 --> 00:11:00,060
So if I run that, we get the same thing.
226
00:11:00,060 --> 00:11:02,100
It's as of Thursday.
227
00:11:02,100 --> 00:11:05,060
But this local variable, I mean, we're really not doing much with this.
228
00:11:05,060 --> 00:11:09,819
So I can actually, where we use today right here, instead, let's just call the method
229
00:11:09,819 --> 00:11:14,020
weekday, which is going to return a string for us, and it'll get substituted right in
230
00:11:14,020 --> 00:11:15,020
there.
231
00:11:15,020 --> 00:11:17,819
And then we can drop that local variable altogether.
232
00:11:17,819 --> 00:11:21,620
Just a quick check, and the movie listing is the same as it was before.
233
00:11:21,620 --> 00:11:23,220
Now suppose we change our mind.
234
00:11:23,220 --> 00:11:25,620
What if we want the weekday printed first?
235
00:11:25,620 --> 00:11:28,820
Well here's the benefit of having all this code inside of one method.
236
00:11:28,820 --> 00:11:32,440
We only have to make the change in one place.
237
00:11:32,440 --> 00:11:34,260
So we're just going to move this around.
238
00:11:34,260 --> 00:11:38,180
We don't want the weekday at the end of this string.
239
00:11:38,180 --> 00:11:40,420
We just want to put it at the beginning.
240
00:11:40,420 --> 00:11:41,460
I'll put a colon there.
241
00:11:41,460 --> 00:11:45,140
And then we want to upcase the weekday, like that.
242
00:11:45,140 --> 00:11:48,660
So we just change that one string in that method movie listing.
243
00:11:48,660 --> 00:11:52,420
If we run it now, we've got exactly what we want, the weekday printed at the beginning
244
00:11:52,420 --> 00:11:53,800
and all uppercased.
245
00:11:53,800 --> 00:11:57,579
And that change rippled through for all of the movie listings.
246
00:11:57,579 --> 00:12:01,459
Now it's important to note here that the method call itself doesn't change.
247
00:12:01,459 --> 00:12:05,199
We just call movie listing passing in the name of the movie and the rank.
248
00:12:05,199 --> 00:12:10,000
So from the outside, it's all the same, which means the changes that we made are isolated
249
00:12:10,000 --> 00:12:12,939
just to the internals of the movie listing method.
250
00:12:12,939 --> 00:12:15,180
So I don't know about you, but that's pretty cool.
251
00:12:15,180 --> 00:12:18,620
We started with a mess, lots of duplication and consistency.
252
00:12:18,620 --> 00:12:22,739
We wrote two methods, and now we have something that's really clean and elegant.
253
00:12:22,739 --> 00:12:23,859
So now it's your turn.
254
00:12:23,859 --> 00:12:27,699
In the exercise, you're going to write your own method for consistently starting the game
255
00:12:27,699 --> 00:12:28,699
for your players.
256
00:12:28,699 --> 00:12:32,300
You'll also get a chance to practice with a few built-in Ruby methods.
257
00:12:32,300 --> 00:12:35,060
And in the next section, we'll take things a step further.
258
00:12:35,060 --> 00:12:38,859
You could write programs composed of nothing but a bunch of small methods, but you'd be
259
00:12:38,859 --> 00:12:42,579
missing a really key aspect of object-oriented programming.
260
00:12:42,580 --> 00:12:47,180
So in the next section, we'll look at how to define classes that help us then create
261
00:12:47,180 --> 00:12:48,380
objects.
262
00:12:48,380 --> 00:12:49,380
So we'll see you then.
263
00:12:49,380 --> 00:13:13,380
Bye.
23879
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.