All language subtitles for pragstudio-ruby-11-separate-files (Transcribed on 24-Apr-2023 20-58-44)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.