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

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
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:06,600 Now you may not realize it, but you've actually come a long ways. 2 00:00:06,600 --> 00:00:10,880 We've learned about writing methods, defining classes, and even putting objects in an array. 3 00:00:10,880 --> 00:00:14,640 Yeah, and we've focused on each of those in isolation to this point, but now we want 4 00:00:14,640 --> 00:00:16,680 to put all of this together. 5 00:00:16,680 --> 00:00:19,460 We're really getting to the heart of OO programming now. 6 00:00:19,460 --> 00:00:24,100 You used your player class to create player objects, and we've used the movie class to 7 00:00:24,100 --> 00:00:26,220 create movie objects. 8 00:00:26,220 --> 00:00:30,880 And in OO programs, what we really want is different kinds of objects that interact with 9 00:00:30,880 --> 00:00:31,880 each other. 10 00:00:31,880 --> 00:00:36,280 Ultimately, we end up with a program where objects collaborate to get something bigger 11 00:00:36,280 --> 00:00:37,280 done. 12 00:00:37,280 --> 00:00:39,360 So let's return to our movie app. 13 00:00:39,360 --> 00:00:40,840 So here's where our code stands. 14 00:00:40,840 --> 00:00:45,640 We have these movie objects all created from a movie class, all neatly encapsulated what 15 00:00:45,640 --> 00:00:46,640 they do. 16 00:00:46,640 --> 00:00:50,160 And then at the bottom here, we have all this stray code that really doesn't have a home 17 00:00:50,160 --> 00:00:51,160 right now. 18 00:00:51,160 --> 00:00:53,019 So this code is trying to tell us something. 19 00:00:53,020 --> 00:00:56,800 We've got some things that are neatly encapsulated and another thing that's kind of like lying 20 00:00:56,800 --> 00:00:58,260 out here on its own. 21 00:00:58,260 --> 00:01:02,920 So who's responsible for maintaining this list of movies and ultimately playing them? 22 00:01:02,920 --> 00:01:07,420 Well, the goal of OO programming is to identify categories of things and make objects that 23 00:01:07,420 --> 00:01:09,120 represent those things. 24 00:01:09,120 --> 00:01:13,620 In this case, we're missing an object and a class to create that object, something that 25 00:01:13,620 --> 00:01:16,200 can store and play movies. 26 00:01:16,200 --> 00:01:19,440 Now what are we going to call that class, and what methods does it have? 27 00:01:19,440 --> 00:01:23,120 Yeah, naming things can often be the hardest part of programming. 28 00:01:23,120 --> 00:01:24,759 Well let's see. 29 00:01:24,759 --> 00:01:28,640 We want to play our movies and we have a list of movies. 30 00:01:28,640 --> 00:01:30,039 Well we could call it a playlist. 31 00:01:30,039 --> 00:01:31,039 Yeah. 32 00:01:31,039 --> 00:01:32,039 Right? 33 00:01:32,039 --> 00:01:34,080 So let's just go ahead and we'll work through the interface that we want and then we'll 34 00:01:34,080 --> 00:01:36,640 flush out the names as we go. 35 00:01:36,640 --> 00:01:40,000 So we want to end up replacing all this code, so I'm just going to delete it. 36 00:01:40,000 --> 00:01:42,120 And let's just say we want something like a playlist. 37 00:01:42,120 --> 00:01:45,160 We're going to put it in a playlist one variable. 38 00:01:45,160 --> 00:01:47,200 The name of the class will be called playlist. 39 00:01:47,200 --> 00:01:51,480 We want to be able to create playlist objects, so we call the new method there. 40 00:01:51,480 --> 00:01:54,680 And then I guess a playlist should have some name. 41 00:01:54,680 --> 00:01:56,520 It's like so-and-so's playlist. 42 00:01:56,520 --> 00:01:58,980 Maybe it's Kermit's playlist in this case. 43 00:01:58,980 --> 00:02:01,000 So we know what the class name is. 44 00:02:01,000 --> 00:02:04,720 Now what should that class actually do or objects of that class do? 45 00:02:04,720 --> 00:02:09,720 Well we want to be able to call a method on that object and ultimately this playlist should 46 00:02:09,720 --> 00:02:11,360 have a list of movies. 47 00:02:11,360 --> 00:02:15,680 So maybe we'll have a method called addMovie and then we can just pass in a movie object 48 00:02:15,680 --> 00:02:17,760 like movie1. 49 00:02:17,760 --> 00:02:19,960 And then we could add in all of our movies here. 50 00:02:19,960 --> 00:02:21,560 Let's take that. 51 00:02:21,560 --> 00:02:25,800 Movie2, movie3 because Kermit likes all of those. 52 00:02:25,800 --> 00:02:29,320 And then when we're done or when we've got all the movies in the playlist, then we just 53 00:02:29,320 --> 00:02:32,560 want to be able to turn around and call the play method. 54 00:02:32,560 --> 00:02:34,440 Now we don't actually have this code yet. 55 00:02:34,440 --> 00:02:37,040 We've just worked through the interface that we want. 56 00:02:37,040 --> 00:02:41,600 That's right, but it does remind me of the tell don't ask principle again. 57 00:02:41,600 --> 00:02:44,200 We're telling the playlist what to do here. 58 00:02:44,200 --> 00:02:47,280 And from this perspective, we don't care how it does it. 59 00:02:47,280 --> 00:02:51,480 We refer to this as encapsulation and it's a good thing in software design. 60 00:02:51,480 --> 00:02:55,600 How the movies are stored inside of the playlist is the playlist's responsibility. 61 00:02:55,600 --> 00:02:59,640 We could implement it to store them in an array or maybe in a file or later on maybe 62 00:02:59,640 --> 00:03:00,839 in a database. 63 00:03:00,839 --> 00:03:06,200 The whole point is it's encapsulated inside the playlist so we have one point of change 64 00:03:06,200 --> 00:03:09,760 if we want to change our mind about how those are stored later. 65 00:03:09,760 --> 00:03:13,320 So now that we know what we want, we can go implement the playlist class. 66 00:03:13,320 --> 00:03:15,760 It's a simple container class for movie objects. 67 00:03:15,760 --> 00:03:18,200 It has its own behavior and it has a name. 68 00:03:18,200 --> 00:03:21,560 Yeah, we'll just implement it up here underneath the movie. 69 00:03:21,560 --> 00:03:23,840 We'll have our class playlist. 70 00:03:23,840 --> 00:03:26,079 It ends in end, right? 71 00:03:26,079 --> 00:03:29,440 When we called playlist.new, we're passing in the name of the playlist. 72 00:03:29,440 --> 00:03:32,840 So we know we're going to need an initialized method to set up the state. 73 00:03:32,840 --> 00:03:36,519 It's going to take a parameter which will be the name and we'll just store that off 74 00:03:36,519 --> 00:03:38,720 or transfer it over into an instance variable. 75 00:03:38,720 --> 00:03:41,000 We're also going to need to store our movies. 76 00:03:41,000 --> 00:03:43,560 In this case, we're just going to store them in an array. 77 00:03:43,560 --> 00:03:48,480 When we initialize this playlist, we're just going to have that movies variable be an empty 78 00:03:48,480 --> 00:03:51,000 array because we haven't added any movies yet. 79 00:03:51,000 --> 00:03:53,400 So that'll let us initialize the movie. 80 00:03:53,400 --> 00:03:57,720 We need another instance method called add movie. 81 00:03:57,720 --> 00:03:59,240 This takes a movie object. 82 00:03:59,240 --> 00:04:02,700 I'll just call the variable here, the parameter here movie. 83 00:04:02,700 --> 00:04:04,600 And then we can take our instance variable movies. 84 00:04:04,600 --> 00:04:05,600 That's an array. 85 00:04:05,600 --> 00:04:09,940 And then we'll just use the append operator and we'll just append the movie that was passed 86 00:04:09,940 --> 00:04:11,680 into the method. 87 00:04:11,680 --> 00:04:14,880 And then finally, we need a play method. 88 00:04:14,880 --> 00:04:16,560 Play method doesn't take any parameters. 89 00:04:16,560 --> 00:04:23,640 And I'll just start by having it print out that we're playing so-and-so's playlist like 90 00:04:23,640 --> 00:04:24,640 that. 91 00:04:24,640 --> 00:04:25,880 I'll go ahead and print out the movies. 92 00:04:25,880 --> 00:04:29,920 Remember we can call put s on an array and it's just going to call the to s method on 93 00:04:29,920 --> 00:04:31,500 each of those elements. 94 00:04:31,500 --> 00:04:35,120 And then just as we did before with some of that stray code, we can actually just start 95 00:04:35,120 --> 00:04:36,400 iterating through our movies now. 96 00:04:36,400 --> 00:04:38,640 We use the each iterator to do that. 97 00:04:38,640 --> 00:04:41,599 It takes a block, a block parameter here. 98 00:04:41,599 --> 00:04:45,280 I'll just call movie, although we could call that anything we wanted. 99 00:04:45,280 --> 00:04:49,039 And then inside of the block, we're going to thumbs up the movie. 100 00:04:49,039 --> 00:04:52,000 And then we're just going to turn around and print the movie as well. 101 00:04:52,000 --> 00:04:56,320 So when play is called, it loops through all the movies, thumbs up them once, and then 102 00:04:56,320 --> 00:04:57,320 prints them out. 103 00:04:57,320 --> 00:04:58,320 We'll run it. 104 00:04:58,320 --> 00:05:00,200 We've got Kermit's playlist. 105 00:05:00,200 --> 00:05:01,880 He starts out with a rank of 10. 106 00:05:01,880 --> 00:05:03,440 Ghostbusters has nine. 107 00:05:03,440 --> 00:05:05,440 And then Goldfinger has zero there. 108 00:05:05,440 --> 00:05:06,800 And then we thumbs up them. 109 00:05:06,800 --> 00:05:11,200 So they just increment all the way back up to Goldfinger again. 110 00:05:11,200 --> 00:05:14,040 So here's visually how these objects interact. 111 00:05:14,040 --> 00:05:18,280 We have a playlist object assigned to the playlist one variable. 112 00:05:18,280 --> 00:05:23,600 And its state is composed of a name and an array of three movie objects that we added 113 00:05:23,600 --> 00:05:25,240 to the playlist. 114 00:05:25,240 --> 00:05:31,140 Now remember, arrays hold references to the objects, not the objects themselves. 115 00:05:31,140 --> 00:05:36,520 So the array element simply points to the three movie objects we added to the playlist. 116 00:05:36,520 --> 00:05:39,240 Now the playlist also has some behavior. 117 00:05:39,240 --> 00:05:43,120 We called the add movie method to add in each movie. 118 00:05:43,120 --> 00:05:48,880 And when we call play on the playlist object, it turns to each movie object and calls the 119 00:05:48,880 --> 00:05:50,440 appropriate method. 120 00:05:50,440 --> 00:05:54,320 In this case, we've defined play to call thumbs up. 121 00:05:54,320 --> 00:05:57,840 So each movie's rank is increased by one. 122 00:05:57,840 --> 00:06:01,680 The takeaway here is that objects tell other objects what to do. 123 00:06:01,680 --> 00:06:05,400 And how those objects do it is their own responsibility. 124 00:06:05,400 --> 00:06:07,760 This is the essence of OO programming. 125 00:06:07,760 --> 00:06:13,520 In larger OO programs, you would simply find more objects talking to other objects. 126 00:06:13,520 --> 00:06:16,440 Now that we have a playlist class, we can create more playlist objects. 127 00:06:16,440 --> 00:06:17,440 Yeah, yeah. 128 00:06:17,440 --> 00:06:18,440 Let's create a playlist for Fozzie. 129 00:06:18,440 --> 00:06:19,440 All right, cool. 130 00:06:19,440 --> 00:06:20,440 Let's try that. 131 00:06:20,440 --> 00:06:24,560 I'm just going to collapse this playlist class so we get a little bit more room. 132 00:06:24,560 --> 00:06:27,560 Down here, we'll create playlist two. 133 00:06:27,560 --> 00:06:28,560 And it's going to be... 134 00:06:28,560 --> 00:06:31,520 This will be Fozzie's playlist. 135 00:06:31,520 --> 00:06:33,120 Okay. 136 00:06:33,120 --> 00:06:34,880 And he's a big fan of Goldfinger. 137 00:06:34,880 --> 00:06:37,280 So let's add movie three. 138 00:06:37,280 --> 00:06:38,280 Movie three. 139 00:06:38,280 --> 00:06:39,280 Got it. 140 00:06:39,280 --> 00:06:40,280 All right. 141 00:06:40,280 --> 00:06:41,280 But he likes Gremlins. 142 00:06:41,280 --> 00:06:43,240 So let's add a fourth movie. 143 00:06:43,240 --> 00:06:45,440 Ah, movie four. 144 00:06:45,440 --> 00:06:46,760 Okay. 145 00:06:46,760 --> 00:06:48,719 You want it to be called Gremlins? 146 00:06:48,719 --> 00:06:49,719 Yeah. 147 00:06:49,719 --> 00:06:52,480 What does he give that as an initial... 148 00:06:52,480 --> 00:06:53,480 Oh, he loves it. 149 00:06:53,480 --> 00:06:54,480 So it's like a 15. 150 00:06:54,480 --> 00:06:55,920 Oh, it starts off 15. 151 00:06:55,920 --> 00:06:56,920 Yeah, yeah. 152 00:06:56,920 --> 00:06:57,920 Okay. 153 00:06:57,920 --> 00:06:58,920 All right. 154 00:06:58,920 --> 00:06:59,920 So then we take his playlist two. 155 00:06:59,920 --> 00:07:03,440 We add movie four, this new movie. 156 00:07:03,440 --> 00:07:05,200 So now we can play this playlist. 157 00:07:05,200 --> 00:07:08,080 We'll just take playlist two and we'll call play. 158 00:07:08,080 --> 00:07:09,600 And then we can run that. 159 00:07:09,600 --> 00:07:10,600 Have a look at that. 160 00:07:10,600 --> 00:07:11,600 We've got both playlists here. 161 00:07:11,600 --> 00:07:16,540 Now, the interesting thing is that Goldfinger, which was shared by both of the playlists, 162 00:07:16,540 --> 00:07:18,520 he starts off with a rank of zero. 163 00:07:18,520 --> 00:07:21,320 Kermit thumbs up it, so it's a rank of one. 164 00:07:21,320 --> 00:07:24,860 In Fozzie's playlist, Goldfinger then starts with a rank of one. 165 00:07:24,860 --> 00:07:28,180 And then he thumbs up it, so it ends with a rank of two. 166 00:07:28,180 --> 00:07:33,360 So in other words, the movie three object, Goldfinger, was shared by both the playlists. 167 00:07:33,360 --> 00:07:38,360 And because remember, a variable simply references an object and we pass the movie three variable 168 00:07:38,360 --> 00:07:43,560 to both playlists, then they're both operating on the same movie object. 169 00:07:43,560 --> 00:07:44,560 That's right, Mike. 170 00:07:44,560 --> 00:07:48,000 And this is a really important point, so it's worth taking another look at. 171 00:07:48,000 --> 00:07:51,840 We have playlist one with movies one, two, and three. 172 00:07:51,840 --> 00:07:58,160 And then we created playlist two and added the movie three object, which is also in playlist 173 00:07:58,160 --> 00:07:59,160 one. 174 00:07:59,160 --> 00:08:00,720 And then we added movie four. 175 00:08:00,720 --> 00:08:05,400 So the important thing to note here is that the reference to the movie three object is 176 00:08:05,400 --> 00:08:08,960 in both playlist one and playlist two. 177 00:08:08,960 --> 00:08:15,160 So when we call play on playlist two, movie three's rank is changed from one to two. 178 00:08:15,160 --> 00:08:18,320 Said another way, objects in Ruby are always passed by reference. 179 00:08:18,320 --> 00:08:23,000 For example, when we passed the movie three to playlist two, we really just passed it 180 00:08:23,000 --> 00:08:26,100 a reference to the existing movie three object. 181 00:08:26,100 --> 00:08:28,660 No new objects were created. 182 00:08:28,660 --> 00:08:32,240 Now to keep things straight, you might find it helpful to draw boxes and lines like we've 183 00:08:32,240 --> 00:08:33,240 done here. 184 00:08:33,240 --> 00:08:34,440 No need to get fancy about it. 185 00:08:34,440 --> 00:08:38,600 I recommend just a quick sketch on the back of a napkin with something like a Sharpie. 186 00:08:38,600 --> 00:08:41,040 All right, so now it's your turn. 187 00:08:41,040 --> 00:08:45,400 In the exercise, you're going to create a game class that will hold your players in 188 00:08:45,400 --> 00:08:49,320 the same way we created a playlist to hold the movies. 189 00:08:49,320 --> 00:08:53,040 Now if you're new to OO programming, make sure to take your time with this exercise 190 00:08:53,040 --> 00:08:56,580 and really understand how objects are being passed around. 191 00:08:56,580 --> 00:09:00,520 Now up to this point, we've put all of our code in one Ruby program file, and that's 192 00:09:00,520 --> 00:09:04,400 a good enough way to get started, but we've kind of got a mess on our hands here. 193 00:09:04,400 --> 00:09:08,960 So in the next section, we'll create separate source files to hold onto our different classes. 194 00:09:08,960 --> 00:09:27,360 We'll see you then. 16457

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