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:08,240
Now, so far in the course, we've been happily motoring along in one program file, but we're
2
00:00:08,240 --> 00:00:10,920
actually starting to accumulate quite a bunch of code.
3
00:00:10,920 --> 00:00:13,800
You know, it doesn't even fit on my screen anymore.
4
00:00:13,800 --> 00:00:17,940
So when you're starting out, using one program file is an okay way to go, but once you create
5
00:00:17,940 --> 00:00:21,480
more than one class, it's time to split things into separate files.
6
00:00:21,480 --> 00:00:25,680
Right, and having the classes separate will make them easier to reuse.
7
00:00:25,680 --> 00:00:29,920
It's also going to make them easier to unit test, but that's a topic for a separate section.
8
00:00:29,920 --> 00:00:33,960
So for right now, let's just tidy up the code we have in separate files.
9
00:00:33,960 --> 00:00:37,920
So I've opened up TextMate inside of this Ruby Studio directory, and we see in this little
10
00:00:37,920 --> 00:00:42,120
project drawer that we have one file inside of that, Flix.rb.
11
00:00:42,120 --> 00:00:45,200
And if we look in Flix.rb, we have a couple different things.
12
00:00:45,200 --> 00:00:49,620
We've got two classes, Movie and Playlist, and then we have this sort of driver code
13
00:00:49,620 --> 00:00:50,660
at the bottom.
14
00:00:50,660 --> 00:00:53,360
So we just want to separate these out into different files.
15
00:00:53,360 --> 00:00:54,840
And we're going to start with the Movie class.
16
00:00:54,840 --> 00:00:58,800
I'm just going to un-collapse it there, take all this code that's in Movie.
17
00:00:58,800 --> 00:01:03,120
I'm just going to cut it out of here, and we're going to create a new file.
18
00:01:03,120 --> 00:01:04,360
In the same directory.
19
00:01:04,360 --> 00:01:05,360
In the same directory, right.
20
00:01:05,360 --> 00:01:09,280
And we're just going to call it Movie.rb, all lowercase.
21
00:01:09,280 --> 00:01:11,399
I'm going to paste in that code.
22
00:01:11,399 --> 00:01:16,060
So the convention is Ruby is the class name starts with an uppercase character that maps
23
00:01:16,060 --> 00:01:20,520
to a file name, lowercase characters, Movie.rb.
24
00:01:20,520 --> 00:01:22,679
Let's do the same thing with our Playlist class.
25
00:01:22,679 --> 00:01:28,300
We'll come back here, we'll un-collapse Playlist, take all this code, cut it out of here.
26
00:01:28,300 --> 00:01:36,360
We'll create a new file, call it Playlist.rb, and then just paste that in.
27
00:01:36,360 --> 00:01:42,600
Now Playlist has this dependency on the Movie class because we add movies to the Playlist.
28
00:01:42,600 --> 00:01:47,840
So in order for this to work, we need to require that Movie file.
29
00:01:47,840 --> 00:01:53,120
We do that by using require relative and give it the name of the file Movie.
30
00:01:53,120 --> 00:01:58,000
And what require relative does is it looks for a file Movie that's relative to the current
31
00:01:58,000 --> 00:01:59,720
file, which is Playlist.rb.
32
00:01:59,720 --> 00:02:03,400
So it's going to find it because they're in the same directory, they're relative to each
33
00:02:03,400 --> 00:02:04,400
other.
34
00:02:04,400 --> 00:02:06,720
And then what require does is it loads up that file.
35
00:02:06,720 --> 00:02:12,720
So when this line runs, the Movie class will be defined because that file was loaded.
36
00:02:12,720 --> 00:02:16,400
And then this Playlist class or this Playlist file is standalone.
37
00:02:16,400 --> 00:02:17,960
We can go ahead and run that.
38
00:02:17,960 --> 00:02:22,040
We're not going to get any errors because all the dependencies are satisfied.
39
00:02:22,040 --> 00:02:24,400
Now back to our Flix file, Flix.rb.
40
00:02:24,400 --> 00:02:27,680
What's left is just some driver code here.
41
00:02:27,680 --> 00:02:31,720
So I'm just going to leave this code in here because it doesn't really belong in a class.
42
00:02:31,720 --> 00:02:34,160
This is our main program.
43
00:02:34,160 --> 00:02:37,480
And if we go ahead and run this now, I'm just going to jump out to a command line.
44
00:02:37,480 --> 00:02:38,880
We'll run it on a command line.
45
00:02:38,880 --> 00:02:45,960
If I run Ruby Flix.rb, ooh, I get this error, uninitialized constant movie.
46
00:02:45,960 --> 00:02:48,000
This error bites me more often than not.
47
00:02:48,000 --> 00:02:49,400
Yeah, it's a strange error.
48
00:02:49,400 --> 00:02:52,480
You wouldn't expect it because uninitialized constant movie.
49
00:02:52,480 --> 00:02:57,959
Well, what's actually happening here is we're using a Movie class inside of this file.
50
00:02:57,959 --> 00:03:02,720
And classes in Ruby are constants, and Ruby can't seem to figure out what a movie is.
51
00:03:02,720 --> 00:03:06,399
So we need to use require relative again in this file.
52
00:03:06,399 --> 00:03:10,120
We need to require our Movie file so that it's defined.
53
00:03:10,120 --> 00:03:13,440
And because we're using a Playlist in here as well, we're going to have to require a
54
00:03:13,440 --> 00:03:14,440
Playlist.
55
00:03:14,440 --> 00:03:19,679
So if we do that, jump back out to the command line, we should be able to run this now, and
56
00:03:19,679 --> 00:03:22,459
sure enough, we get both of our Playlists.
57
00:03:22,460 --> 00:03:28,940
Just one more thing to look at is because the Playlist class requires Movie, inside
58
00:03:28,940 --> 00:03:35,760
of Flix.rb, if we just require relative Playlist, then it's also going to require Movie, because
59
00:03:35,760 --> 00:03:37,340
Playlist requires Movie.
60
00:03:37,340 --> 00:03:40,240
So this will just work as well.
61
00:03:40,240 --> 00:03:43,960
So to wrap things up, I just want to show you a little trick here.
62
00:03:43,960 --> 00:03:47,120
Sometimes you want to put some example code at the bottom of your classes.
63
00:03:47,120 --> 00:03:51,200
Let's say in this Movie.rb file, down at the very bottom, I want to just some example code
64
00:03:51,200 --> 00:03:52,940
about how to use a Movie.
65
00:03:52,940 --> 00:03:56,079
We just create a Movie, thumbs up it, print its rank, and so on.
66
00:03:56,079 --> 00:04:02,459
And if we run this file by itself, sure enough, that example code runs.
67
00:04:02,459 --> 00:04:08,079
But if we go over to Flix.rb, remember Movie gets required through the process of requiring
68
00:04:08,079 --> 00:04:09,079
this thing.
69
00:04:09,079 --> 00:04:14,720
So if we run Flix.rb, we get this spurious little code at the top where it's running
70
00:04:14,720 --> 00:04:17,399
our test code that's in our Movie class.
71
00:04:17,399 --> 00:04:18,399
We really don't want that.
72
00:04:18,399 --> 00:04:19,399
So how can we get around that?
73
00:04:19,399 --> 00:04:23,520
Well, there's a neat little trick back in our Movie.rb file.
74
00:04:23,520 --> 00:04:27,919
We only want to run this code if we're running the Movie.rb file.
75
00:04:27,919 --> 00:04:29,760
So we can surround this with an if statement.
76
00:04:29,760 --> 00:04:34,700
We can say if, use underscore, underscore file, underscore, underscore.
77
00:04:34,700 --> 00:04:41,340
That's a variable that holds the file name of this file, which would be Movie.rb.
78
00:04:41,340 --> 00:04:49,320
If Movie.rb equals the currently running program, and that's stored in $0, so when you run Ruby,
79
00:04:49,320 --> 00:04:55,440
Movie.rb, Movie.rb is stored in $0, we can surround all that with that if statement.
80
00:04:55,440 --> 00:05:00,719
And that will guard so that if we run this file directly, well, sure enough, we get the
81
00:05:00,719 --> 00:05:02,080
example code.
82
00:05:02,080 --> 00:05:09,000
But if we then go run Flix.rb, that code won't show up anymore because we're running a separate
83
00:05:09,000 --> 00:05:13,960
file, Flix.rb, and the if statement back over in our Movie class won't fire.
84
00:05:13,960 --> 00:05:17,300
So that's just a little trick if you want to use some example code at the bottom of
85
00:05:17,300 --> 00:05:21,760
your classes, but you don't want them to get run when you run the entire program.
86
00:05:21,760 --> 00:05:25,280
Hey, all of our code now fits on one screen.
87
00:05:25,280 --> 00:05:26,280
Yay!
88
00:05:26,280 --> 00:05:27,280
Yay!
89
00:05:27,280 --> 00:05:29,280
Kidding aside, this is actually something to aim for.
90
00:05:29,280 --> 00:05:32,960
Yeah, if you find yourself scrolling through a lot of code, it may be trying to tell you
91
00:05:32,960 --> 00:05:33,960
something.
92
00:05:33,960 --> 00:05:37,560
Small files and small classes and small methods, they're a good thing.
93
00:05:37,560 --> 00:05:42,400
It makes it easier for you to understand and read, as well as the next programmer.
94
00:05:42,400 --> 00:05:46,940
So take a couple minutes to put your classes into separate files, and then when you come
95
00:05:46,940 --> 00:05:49,040
back, we're going to write some unit tests for the code.
9155
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.