Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now, so far in the course, we've been happily motoring along in one program file, but we're
actually starting to accumulate quite a bunch of code.
You know, it doesn't even fit on my screen anymore.
So when you're starting out, using one program file is an okay way to go, but once you create
more than one class, it's time to split things into separate files.
Right, and having the classes separate will make them easier to reuse.
It's also going to make them easier to unit test, but that's a topic for a separate section.
So for right now, let's just tidy up the code we have in separate files.
So I've opened up TextMate inside of this Ruby Studio directory, and we see in this little
project drawer that we have one file inside of that, Flix.rb.
And if we look in Flix.rb, we have a couple different things.
We've got two classes, Movie and Playlist, and then we have this sort of driver code
at the bottom.
So we just want to separate these out into different files.
And we're going to start with the Movie class.
I'm just going to un-collapse it there, take all this code that's in Movie.
I'm just going to cut it out of here, and we're going to create a new file.
In the same directory.
In the same directory, right.
And we're just going to call it Movie.rb, all lowercase.
I'm going to paste in that code.
So the convention is Ruby is the class name starts with an uppercase character that maps
to a file name, lowercase characters, Movie.rb.
Let's do the same thing with our Playlist class.
We'll come back here, we'll un-collapse Playlist, take all this code, cut it out of here.
We'll create a new file, call it Playlist.rb, and then just paste that in.
Now Playlist has this dependency on the Movie class because we add movies to the Playlist.
So in order for this to work, we need to require that Movie file.
We do that by using require relative and give it the name of the file Movie.
And what require relative does is it looks for a file Movie that's relative to the current
file, which is Playlist.rb.
So it's going to find it because they're in the same directory, they're relative to each
other.
And then what require does is it loads up that file.
So when this line runs, the Movie class will be defined because that file was loaded.
And then this Playlist class or this Playlist file is standalone.
We can go ahead and run that.
We're not going to get any errors because all the dependencies are satisfied.
Now back to our Flix file, Flix.rb.
What's left is just some driver code here.
So I'm just going to leave this code in here because it doesn't really belong in a class.
This is our main program.
And if we go ahead and run this now, I'm just going to jump out to a command line.
We'll run it on a command line.
If I run Ruby Flix.rb, ooh, I get this error, uninitialized constant movie.
This error bites me more often than not.
Yeah, it's a strange error.
You wouldn't expect it because uninitialized constant movie.
Well, what's actually happening here is we're using a Movie class inside of this file.
And classes in Ruby are constants, and Ruby can't seem to figure out what a movie is.
So we need to use require relative again in this file.
We need to require our Movie file so that it's defined.
And because we're using a Playlist in here as well, we're going to have to require a
Playlist.
So if we do that, jump back out to the command line, we should be able to run this now, and
sure enough, we get both of our Playlists.
Just one more thing to look at is because the Playlist class requires Movie, inside
of Flix.rb, if we just require relative Playlist, then it's also going to require Movie, because
Playlist requires Movie.
So this will just work as well.
So to wrap things up, I just want to show you a little trick here.
Sometimes you want to put some example code at the bottom of your classes.
Let's say in this Movie.rb file, down at the very bottom, I want to just some example code
about how to use a Movie.
We just create a Movie, thumbs up it, print its rank, and so on.
And if we run this file by itself, sure enough, that example code runs.
But if we go over to Flix.rb, remember Movie gets required through the process of requiring
this thing.
So if we run Flix.rb, we get this spurious little code at the top where it's running
our test code that's in our Movie class.
We really don't want that.
So how can we get around that?
Well, there's a neat little trick back in our Movie.rb file.
We only want to run this code if we're running the Movie.rb file.
So we can surround this with an if statement.
We can say if, use underscore, underscore file, underscore, underscore.
That's a variable that holds the file name of this file, which would be Movie.rb.
If Movie.rb equals the currently running program, and that's stored in $0, so when you run Ruby,
Movie.rb, Movie.rb is stored in $0, we can surround all that with that if statement.
And that will guard so that if we run this file directly, well, sure enough, we get the
example code.
But if we then go run Flix.rb, that code won't show up anymore because we're running a separate
file, Flix.rb, and the if statement back over in our Movie class won't fire.
So that's just a little trick if you want to use some example code at the bottom of
your classes, but you don't want them to get run when you run the entire program.
Hey, all of our code now fits on one screen.
Yay!
Yay!
Kidding aside, this is actually something to aim for.
Yeah, if you find yourself scrolling through a lot of code, it may be trying to tell you
something.
Small files and small classes and small methods, they're a good thing.
It makes it easier for you to understand and read, as well as the next programmer.
So take a couple minutes to put your classes into separate files, and then when you come
back, we're going to write some unit tests for the code.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.