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:04,920
Okay, welcome back.
2
00:00:04,920 --> 00:00:07,360
We want to talk a little bit about design in this section.
3
00:00:07,360 --> 00:00:11,000
Yeah, I was looking at our playlist class, and it currently has a little bit too much
4
00:00:11,000 --> 00:00:13,720
responsibility, so let's change that around a little bit.
5
00:00:13,720 --> 00:00:14,720
Okay.
6
00:00:14,720 --> 00:00:19,020
In the play method here, we see we're looping through all the movies, but we're also rolling
7
00:00:19,020 --> 00:00:22,960
this die, and then we're either thumbs-upping or thumbs-down the movie.
8
00:00:22,960 --> 00:00:26,420
This doesn't feel like it should be the responsibility of a playlist to do.
9
00:00:26,420 --> 00:00:28,320
It should just be able to play the movies.
10
00:00:28,320 --> 00:00:29,320
Right.
11
00:00:29,320 --> 00:00:33,320
And in the same way, the play method in your game is probably doing too much.
12
00:00:33,320 --> 00:00:36,920
So I guess the question is, who's responsible for this logic?
13
00:00:36,920 --> 00:00:40,240
It doesn't really make sense to add it to the movie class, because a movie's not going
14
00:00:40,240 --> 00:00:41,880
to review itself.
15
00:00:41,880 --> 00:00:45,720
So we need something that encapsulates the concept of a reviewer of some sort.
16
00:00:45,720 --> 00:00:46,720
Yeah, we need a reviewer.
17
00:00:46,720 --> 00:00:50,200
Hey, how about we get Waldorf and Statler to review our movies?
18
00:00:50,200 --> 00:00:51,200
You know them.
19
00:00:51,200 --> 00:00:52,200
They're from The Muppets.
20
00:00:52,200 --> 00:00:55,400
Oh, those guys would be perfect, because they're cranky and cantankerous both.
21
00:00:55,400 --> 00:00:56,400
Yeah.
22
00:00:56,400 --> 00:00:59,320
So let's have them responsible for reviewing movies for us.
23
00:00:59,320 --> 00:01:03,920
Okay, so if we give them a responsibility of thumbs-up and thumbs-down, we could use
24
00:01:03,920 --> 00:01:06,200
a class, but we don't have any objects.
25
00:01:06,200 --> 00:01:09,620
Well, and I don't think a reviewer is going to have any state initially either.
26
00:01:09,620 --> 00:01:13,240
We just basically need a method that takes a movie, and then it will review that.
27
00:01:13,240 --> 00:01:14,240
Right.
28
00:01:14,240 --> 00:01:15,800
So instead of a class, let's use a module.
29
00:01:15,800 --> 00:01:16,880
Let's do that.
30
00:01:16,880 --> 00:01:19,560
So I'll just create a file inside of here.
31
00:01:19,560 --> 00:01:21,620
I like that name, Waldorf and Statler.
32
00:01:21,620 --> 00:01:27,260
So we'll just call the file Waldorf and Statler, like that.
33
00:01:27,260 --> 00:01:29,520
And then inside of here, we want to define a module.
34
00:01:29,520 --> 00:01:34,280
Now a module is like a class, but we start it with the module keyword, and it's going
35
00:01:34,280 --> 00:01:37,640
to be the Waldorf and Statler module.
36
00:01:37,640 --> 00:01:38,820
Now notice the naming here.
37
00:01:38,820 --> 00:01:43,300
We use this camel case for the actual name of the module, but then the file name, we
38
00:01:43,300 --> 00:01:45,680
separate the words with underscores.
39
00:01:45,680 --> 00:01:47,420
So that's just a convention in Ruby.
40
00:01:47,420 --> 00:01:50,540
And then we can end the module just like that.
41
00:01:50,540 --> 00:01:53,120
So unlike classes, you don't instantiate modules.
42
00:01:53,120 --> 00:01:54,960
In fact, you can't instantiate modules.
43
00:01:54,960 --> 00:01:59,560
If we try to instantiate this now and call the new method to get an object out of here,
44
00:01:59,560 --> 00:02:03,020
well we get this error, undefined method new for a module.
45
00:02:03,020 --> 00:02:04,860
So what's a module good for?
46
00:02:04,860 --> 00:02:09,400
Well later we'll use modules as mix-ins and namespaces, but first we'll just use a module
47
00:02:09,400 --> 00:02:11,580
as a bucket of related methods.
48
00:02:11,580 --> 00:02:13,300
So let's add a method in here.
49
00:02:13,300 --> 00:02:20,520
We'll call it review, and I'll just print out bravo, just like that.
50
00:02:20,520 --> 00:02:22,380
But we know that this is an instance method.
51
00:02:22,380 --> 00:02:26,080
If we were to have this method inside of a class, we would need an instance of the class
52
00:02:26,080 --> 00:02:27,400
to actually call that method.
53
00:02:27,400 --> 00:02:32,100
So there's no way to call this method right now because we can't create an instance of
54
00:02:32,100 --> 00:02:33,140
the module.
55
00:02:33,140 --> 00:02:37,480
So instead we need to make this method into what's called a module method.
56
00:02:37,480 --> 00:02:40,280
And we do that using the self variable.
57
00:02:40,280 --> 00:02:47,440
Self.review creates this method on self, and in this particular context, self is the module.
58
00:02:47,440 --> 00:02:52,240
So the way we call this method is we can just use the module down here and call the method
59
00:02:52,240 --> 00:02:53,240
directly.
60
00:02:53,240 --> 00:02:55,000
So we could just call review, just like that.
61
00:02:55,000 --> 00:02:57,579
We don't need an object, don't need to call new at all.
62
00:02:57,579 --> 00:03:00,880
It's just a module level method that we can call from the module.
63
00:03:00,880 --> 00:03:04,400
If we run that, sure enough, we get bravo.
64
00:03:04,400 --> 00:03:06,400
But there's just one problem.
65
00:03:06,400 --> 00:03:08,960
Waldorf and Statler, they would never say bravo.
66
00:03:08,960 --> 00:03:11,920
Yeah, that's entirely too positive for them to say.
67
00:03:11,920 --> 00:03:16,120
So I guess we need to change this around to actually do something with our movies now.
68
00:03:16,120 --> 00:03:18,120
So let's go back over to our playlist class.
69
00:03:18,120 --> 00:03:21,360
And what we want to do is basically move this code over into there.
70
00:03:21,360 --> 00:03:25,160
So I'm just going to take, let's see, let's take this code over.
71
00:03:25,160 --> 00:03:27,680
Actually, we want to leave the put us at the bottom.
72
00:03:27,680 --> 00:03:32,080
So let's take this code and we want to go back over into our module.
73
00:03:32,080 --> 00:03:35,360
And inside of review, we'll just paste that code in.
74
00:03:35,360 --> 00:03:39,640
We definitely need to pass a movie into this method because we use a movie down here.
75
00:03:39,640 --> 00:03:42,600
Oh, and we also have this method roll die.
76
00:03:42,600 --> 00:03:46,880
So let's go back over to our playlist and grab that as well.
77
00:03:46,880 --> 00:03:49,280
Because that's how Waldorf and Statler are actually going to review the movies.
78
00:03:49,280 --> 00:03:50,880
They're just going to roll this die.
79
00:03:50,880 --> 00:03:53,440
Well, unless Miss Piggy's in it.
80
00:03:53,440 --> 00:03:55,600
Well, that could be kind of fun.
81
00:03:55,600 --> 00:03:59,200
Now this method roll die here has to be a module method as well, because we're going
82
00:03:59,200 --> 00:04:02,959
to call it inside of this module method called roll die.
83
00:04:02,959 --> 00:04:07,440
So we need to use the self.syntax here too to make that a module method.
84
00:04:07,440 --> 00:04:08,680
So we've got our movie.
85
00:04:08,680 --> 00:04:10,400
We've got our number roll variable.
86
00:04:10,400 --> 00:04:13,320
The rest of this code stays exactly the same.
87
00:04:13,320 --> 00:04:14,480
We'll save it.
88
00:04:14,480 --> 00:04:16,000
We need to go back to our playlist.
89
00:04:16,000 --> 00:04:20,700
Now right here where we took out that code, actually it's inside the loop for movies,
90
00:04:20,700 --> 00:04:24,600
we want to call our Waldorf and Statler module.
91
00:04:24,600 --> 00:04:26,200
We can call our module method review.
92
00:04:26,200 --> 00:04:28,880
We want to pass in the movie.
93
00:04:28,880 --> 00:04:32,060
And remember, now we have this dependency on this module.
94
00:04:32,060 --> 00:04:38,920
So at the top of the file, we need to require relative the Waldorf and Statler module.
95
00:04:38,920 --> 00:04:40,320
Just like that.
96
00:04:40,320 --> 00:04:44,719
So now let's go run our flix.rb file and just run that.
97
00:04:44,719 --> 00:04:46,740
Ooh, we have an error here.
98
00:04:46,740 --> 00:04:52,440
On method review, we're taking a movie in there, but right down here at the bottom,
99
00:04:52,440 --> 00:04:55,680
I left in this call to Waldorf and Statler.review.
100
00:04:55,680 --> 00:04:57,960
Remember when we require this file, it's going to run all the code.
101
00:04:57,960 --> 00:05:02,280
So we've got to take that little bit of example code out of the bottom.
102
00:05:02,280 --> 00:05:03,800
Just double check our playlist.
103
00:05:03,800 --> 00:05:06,700
Yep, it's calling review, passing in a movie.
104
00:05:06,700 --> 00:05:08,039
So we should be good now.
105
00:05:08,039 --> 00:05:09,560
So let's run it again.
106
00:05:09,560 --> 00:05:13,400
We've got our two playlists, and it looks basically the same as it did before.
107
00:05:13,400 --> 00:05:19,280
The only difference is we've moved the responsibility of who reviews this movie over to the module.
108
00:05:19,280 --> 00:05:22,140
From this outside, we're getting the same results.
109
00:05:22,140 --> 00:05:24,240
So let's check our specs now.
110
00:05:24,240 --> 00:05:28,280
If we go over to our playlists spec.rb, let's just go ahead and run that.
111
00:05:28,280 --> 00:05:29,880
Ooh, now we get a problem.
112
00:05:29,880 --> 00:05:34,500
And if we look through here, we kind of notice that it's looking sort of like random again.
113
00:05:34,500 --> 00:05:36,960
The first one failed and the second two of them succeeded.
114
00:05:36,960 --> 00:05:41,280
If I run it again, well, they all failed this time.
115
00:05:41,280 --> 00:05:42,280
So what's going on there?
116
00:05:42,280 --> 00:05:45,799
Well, it sounds like we're getting this random behavior out of a die again.
117
00:05:45,799 --> 00:05:47,000
Let's look at our spec.
118
00:05:47,000 --> 00:05:52,320
Oh, remember, we were stubbing out this call to Roll Die, and we were stubbing it out on
119
00:05:52,320 --> 00:05:54,200
the playlist object here.
120
00:05:54,200 --> 00:05:58,840
Well, we don't want to do that anymore because we've moved the Roll Die method over into
121
00:05:58,840 --> 00:06:01,120
the Waldorf and Statler module.
122
00:06:01,120 --> 00:06:04,000
That's right.
123
00:06:04,000 --> 00:06:06,200
So if we're going to stub it, we've got to stub it in the right place.
124
00:06:06,200 --> 00:06:07,200
I'm just going to take that.
125
00:06:07,200 --> 00:06:08,200
We'll put it down here.
126
00:06:08,200 --> 00:06:12,280
And I think there's one more spot right here where we call it.
127
00:06:12,280 --> 00:06:13,280
Paste that in.
128
00:06:13,280 --> 00:06:16,159
And now we should be able to run this.
129
00:06:16,159 --> 00:06:18,280
Sure enough, all of our tests are now passing.
130
00:06:18,280 --> 00:06:22,800
So we've successfully done the refactoring, moved the code over, and then we run our tests
131
00:06:22,800 --> 00:06:27,080
at the end, make sure they all pass so that we know we didn't break anything.
132
00:06:27,080 --> 00:06:31,560
Now we did a lot of refactoring in this section, so should we run all of our tests?
133
00:06:31,560 --> 00:06:32,680
That's actually a really good idea.
134
00:06:32,680 --> 00:06:36,480
Let me just go back over to the command line and we can run all the specs.
135
00:06:36,480 --> 00:06:40,700
We've got a couple of them in here, rspec dot, and we'll add in some color.
136
00:06:40,700 --> 00:06:42,120
And sure enough, we're all good.
137
00:06:42,120 --> 00:06:43,120
All right.
138
00:06:43,120 --> 00:06:47,240
Now in the exercise, you're going to get to do a similar refactoring with the game.
139
00:06:47,240 --> 00:06:49,780
It'll be your chance to write your first Ruby module.
140
00:06:49,780 --> 00:06:52,160
Now we haven't seen the last of modules.
141
00:06:52,160 --> 00:06:56,300
Now we're going to use modules a little later on as mix-ins, a unique way to share code
142
00:06:56,300 --> 00:06:57,300
in Ruby.
143
00:06:57,300 --> 00:07:00,660
We're also going to use modules as a way of namespacing our code.
144
00:07:00,660 --> 00:07:04,440
But for now, just think of modules as buckets of methods.
145
00:07:04,440 --> 00:07:07,460
So give this exercise a go, and then when you come back, we're going to dive deeper
146
00:07:07,460 --> 00:07:31,359
into blocks and iterators.
12832
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.