All language subtitles for pragstudio-ruby-10-objects-interacting (Transcribed on 27-Apr-2023 20-40-32)

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 you may not realize it, but you've actually come a long ways.

We've learned about writing methods, defining classes, and even putting objects in an array.

Yeah, and we've focused on each of those in isolation to this point, but now we want

to put all of this together.

We're really getting to the heart of OO programming now.

You used your player class to create player objects, and we've used the movie class to

create movie objects.

And in OO programs, what we really want is different kinds of objects that interact with

each other.

Ultimately, we end up with a program where objects collaborate to get something bigger

done.

So let's return to our movie app.

So here's where our code stands.

We have these movie objects all created from a movie class, all neatly encapsulated what

they do.

And then at the bottom here, we have all this stray code that really doesn't have a home

right now.

So this code is trying to tell us something.

We've got some things that are neatly encapsulated and another thing that's kind of like lying

out here on its own.

So who's responsible for maintaining this list of movies and ultimately playing them?

Well, the goal of OO programming is to identify categories of things and make objects that

represent those things.

In this case, we're missing an object and a class to create that object, something that

can store and play movies.

Now what are we going to call that class, and what methods does it have?

Yeah, naming things can often be the hardest part of programming.

Well let's see.

We want to play our movies and we have a list of movies.

Well we could call it a playlist.

Yeah.

Right?

So let's just go ahead and we'll work through the interface that we want and then we'll

flush out the names as we go.

So we want to end up replacing all this code, so I'm just going to delete it.

And let's just say we want something like a playlist.

We're going to put it in a playlist one variable.

The name of the class will be called playlist.

We want to be able to create playlist objects, so we call the new method there.

And then I guess a playlist should have some name.

It's like so-and-so's playlist.

Maybe it's Kermit's playlist in this case.

So we know what the class name is.

Now what should that class actually do or objects of that class do?

Well we want to be able to call a method on that object and ultimately this playlist should

have a list of movies.

So maybe we'll have a method called addMovie and then we can just pass in a movie object

like movie1.

And then we could add in all of our movies here.

Let's take that.

Movie2, movie3 because Kermit likes all of those.

And then when we're done or when we've got all the movies in the playlist, then we just

want to be able to turn around and call the play method.

Now we don't actually have this code yet.

We've just worked through the interface that we want.

That's right, but it does remind me of the tell don't ask principle again.

We're telling the playlist what to do here.

And from this perspective, we don't care how it does it.

We refer to this as encapsulation and it's a good thing in software design.

How the movies are stored inside of the playlist is the playlist's responsibility.

We could implement it to store them in an array or maybe in a file or later on maybe

in a database.

The whole point is it's encapsulated inside the playlist so we have one point of change

if we want to change our mind about how those are stored later.

So now that we know what we want, we can go implement the playlist class.

It's a simple container class for movie objects.

It has its own behavior and it has a name.

Yeah, we'll just implement it up here underneath the movie.

We'll have our class playlist.

It ends in end, right?

When we called playlist.new, we're passing in the name of the playlist.

So we know we're going to need an initialized method to set up the state.

It's going to take a parameter which will be the name and we'll just store that off

or transfer it over into an instance variable.

We're also going to need to store our movies.

In this case, we're just going to store them in an array.

When we initialize this playlist, we're just going to have that movies variable be an empty

array because we haven't added any movies yet.

So that'll let us initialize the movie.

We need another instance method called add movie.

This takes a movie object.

I'll just call the variable here, the parameter here movie.

And then we can take our instance variable movies.

That's an array.

And then we'll just use the append operator and we'll just append the movie that was passed

into the method.

And then finally, we need a play method.

Play method doesn't take any parameters.

And I'll just start by having it print out that we're playing so-and-so's playlist like

that.

I'll go ahead and print out the movies.

Remember we can call put s on an array and it's just going to call the to s method on

each of those elements.

And then just as we did before with some of that stray code, we can actually just start

iterating through our movies now.

We use the each iterator to do that.

It takes a block, a block parameter here.

I'll just call movie, although we could call that anything we wanted.

And then inside of the block, we're going to thumbs up the movie.

And then we're just going to turn around and print the movie as well.

So when play is called, it loops through all the movies, thumbs up them once, and then

prints them out.

We'll run it.

We've got Kermit's playlist.

He starts out with a rank of 10.

Ghostbusters has nine.

And then Goldfinger has zero there.

And then we thumbs up them.

So they just increment all the way back up to Goldfinger again.

So here's visually how these objects interact.

We have a playlist object assigned to the playlist one variable.

And its state is composed of a name and an array of three movie objects that we added

to the playlist.

Now remember, arrays hold references to the objects, not the objects themselves.

So the array element simply points to the three movie objects we added to the playlist.

Now the playlist also has some behavior.

We called the add movie method to add in each movie.

And when we call play on the playlist object, it turns to each movie object and calls the

appropriate method.

In this case, we've defined play to call thumbs up.

So each movie's rank is increased by one.

The takeaway here is that objects tell other objects what to do.

And how those objects do it is their own responsibility.

This is the essence of OO programming.

In larger OO programs, you would simply find more objects talking to other objects.

Now that we have a playlist class, we can create more playlist objects.

Yeah, yeah.

Let's create a playlist for Fozzie.

All right, cool.

Let's try that.

I'm just going to collapse this playlist class so we get a little bit more room.

Down here, we'll create playlist two.

And it's going to be...

This will be Fozzie's playlist.

Okay.

And he's a big fan of Goldfinger.

So let's add movie three.

Movie three.

Got it.

All right.

But he likes Gremlins.

So let's add a fourth movie.

Ah, movie four.

Okay.

You want it to be called Gremlins?

Yeah.

What does he give that as an initial...

Oh, he loves it.

So it's like a 15.

Oh, it starts off 15.

Yeah, yeah.

Okay.

All right.

So then we take his playlist two.

We add movie four, this new movie.

So now we can play this playlist.

We'll just take playlist two and we'll call play.

And then we can run that.

Have a look at that.

We've got both playlists here.

Now, the interesting thing is that Goldfinger, which was shared by both of the playlists,

he starts off with a rank of zero.

Kermit thumbs up it, so it's a rank of one.

In Fozzie's playlist, Goldfinger then starts with a rank of one.

And then he thumbs up it, so it ends with a rank of two.

So in other words, the movie three object, Goldfinger, was shared by both the playlists.

And because remember, a variable simply references an object and we pass the movie three variable

to both playlists, then they're both operating on the same movie object.

That's right, Mike.

And this is a really important point, so it's worth taking another look at.

We have playlist one with movies one, two, and three.

And then we created playlist two and added the movie three object, which is also in playlist

one.

And then we added movie four.

So the important thing to note here is that the reference to the movie three object is

in both playlist one and playlist two.

So when we call play on playlist two, movie three's rank is changed from one to two.

Said another way, objects in Ruby are always passed by reference.

For example, when we passed the movie three to playlist two, we really just passed it

a reference to the existing movie three object.

No new objects were created.

Now to keep things straight, you might find it helpful to draw boxes and lines like we've

done here.

No need to get fancy about it.

I recommend just a quick sketch on the back of a napkin with something like a Sharpie.

All right, so now it's your turn.

In the exercise, you're going to create a game class that will hold your players in

the same way we created a playlist to hold the movies.

Now if you're new to OO programming, make sure to take your time with this exercise

and really understand how objects are being passed around.

Now up to this point, we've put all of our code in one Ruby program file, and that's

a good enough way to get started, but we've kind of got a mess on our hands here.

So in the next section, we'll create separate source files to hold onto our different classes.

We'll see you then.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.