All language subtitles for pragstudio-ruby-07-classes (Transcribed on 24-Apr-2023 20-59-05)

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
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:04,520 Hey, welcome back. 2 00:00:04,520 --> 00:00:08,600 In the exercise you just finished, you ended up writing a method that printed out a player's 3 00:00:08,600 --> 00:00:09,600 name and health. 4 00:00:09,600 --> 00:00:10,600 Right. 5 00:00:10,600 --> 00:00:13,080 It looked something like this. 6 00:00:13,080 --> 00:00:17,740 Now let's look at that in a slightly different way. 7 00:00:17,740 --> 00:00:22,600 This code actually does the same thing, but we've used local variables to hold onto the 8 00:00:22,600 --> 00:00:24,500 player's name and health. 9 00:00:24,500 --> 00:00:29,160 We can think of these variables as representing the player's current state. 10 00:00:29,160 --> 00:00:32,800 Your method then does something with the player's state, it prints it out. 11 00:00:32,800 --> 00:00:35,880 We can think of that as the player's behavior. 12 00:00:35,880 --> 00:00:39,480 That's kind of cool, but right now the state and behavior are just loosely associated. 13 00:00:39,480 --> 00:00:42,040 It'd be nice to wrap them together. 14 00:00:42,040 --> 00:00:44,880 And that's exactly what classes let us do. 15 00:00:44,880 --> 00:00:48,540 With a class, we can combine variables and the methods together. 16 00:00:48,540 --> 00:00:52,540 Or said another way, we can combine state and behavior together. 17 00:00:52,540 --> 00:00:57,360 Then we can use that class as a template to create objects and fill in the values for 18 00:00:57,360 --> 00:00:59,240 the state. 19 00:00:59,240 --> 00:01:03,640 Here's a player object that represents Larry with a health of 60. 20 00:01:03,640 --> 00:01:08,720 And here's another player object representing Mo with a health of 100. 21 00:01:08,720 --> 00:01:12,539 Notice that each object's state is unique, but they have the same behavior. 22 00:01:12,539 --> 00:01:14,600 Each player can say hello. 23 00:01:14,600 --> 00:01:18,940 So let's recap how that works with Ruby classes that we've already used. 24 00:01:18,940 --> 00:01:23,000 So back over in IRB, let's try out some of these classes and objects. 25 00:01:23,000 --> 00:01:26,920 So we can think of a class as a factory that just churns out objects. 26 00:01:26,920 --> 00:01:30,800 If we had a variable called greeting, for example, to create a string object, we're 27 00:01:30,800 --> 00:01:34,960 used to using the literal, either a double quoted string or a single quoted string. 28 00:01:34,960 --> 00:01:36,360 I'll just say hello. 29 00:01:36,360 --> 00:01:41,540 And if we look at the object's class, well, we have a string class. 30 00:01:41,540 --> 00:01:46,520 The object referenced by the greeting variable was created from the string class. 31 00:01:46,520 --> 00:01:51,280 Now a more explicit way to create a string is to use the string class itself. 32 00:01:51,280 --> 00:01:55,400 We can call the new method on that class and pass in the actual characters. 33 00:01:55,400 --> 00:01:57,520 So we're gonna get exactly the same thing. 34 00:01:57,520 --> 00:01:59,960 We have a string called hello. 35 00:01:59,960 --> 00:02:05,880 We often refer to objects like this as instances because they're instances of the specific 36 00:02:05,880 --> 00:02:06,880 class. 37 00:02:06,880 --> 00:02:09,040 They were created from a class, in this case, string. 38 00:02:09,040 --> 00:02:11,340 Now we've seen that with objects, we can also call methods. 39 00:02:11,340 --> 00:02:15,760 So if I have greeting, I could call reverse, for example, to reverse the string. 40 00:02:15,760 --> 00:02:20,280 Or I could use upcase to uppercase the string. 41 00:02:20,280 --> 00:02:21,660 So that's one string object. 42 00:02:21,660 --> 00:02:22,660 Let's try another one. 43 00:02:22,660 --> 00:02:26,560 In this case, we'll have an object called farewell or a variable called farewell. 44 00:02:26,560 --> 00:02:28,960 And we'll create it using the explicit way. 45 00:02:28,960 --> 00:02:32,680 We'll just pass in goodbye, like so. 46 00:02:32,680 --> 00:02:35,920 So now we've got this string has the characters goodbye. 47 00:02:35,920 --> 00:02:40,380 But we can also call the methods reverse, for example, to reverse it. 48 00:02:40,380 --> 00:02:43,640 And we can call the method upcase to upcase it. 49 00:02:43,640 --> 00:02:49,480 So objects created from the same class have unique state, hello and goodbye, but the same 50 00:02:49,480 --> 00:02:51,720 shared behavior, reverse and upcase. 51 00:02:51,720 --> 00:02:53,040 Yeah, that's right. 52 00:02:53,040 --> 00:02:54,600 They also have a unique object ID. 53 00:02:54,600 --> 00:02:59,200 If we look at our greeting object, if we look at the object ID for that, and then we look 54 00:02:59,200 --> 00:03:04,000 at the farewell objects ID, they're two separate objects in memory. 55 00:03:04,000 --> 00:03:06,480 And just to drive this home, let's try just one more example. 56 00:03:06,480 --> 00:03:08,520 Let's say we have a time object. 57 00:03:08,520 --> 00:03:10,400 I'll call it t1. 58 00:03:10,400 --> 00:03:12,600 We use time.new to get that object. 59 00:03:12,600 --> 00:03:15,100 So that's got the current time when I type that in. 60 00:03:15,100 --> 00:03:20,800 It has methods like what is the current second or is it Monday, for example. 61 00:03:20,800 --> 00:03:23,600 But we haven't yet created a t2 object, Mike. 62 00:03:23,600 --> 00:03:26,640 Oh, that's supposed to be the t1 object, actually. 63 00:03:26,640 --> 00:03:29,520 t1, it's not Monday, either way we do it. 64 00:03:29,520 --> 00:03:31,420 All right, so we've got a t1 object. 65 00:03:31,420 --> 00:03:33,720 Let's try another time object. 66 00:03:33,720 --> 00:03:36,160 t2, we can ask it. 67 00:03:36,160 --> 00:03:37,320 It's got a unique state. 68 00:03:37,320 --> 00:03:38,880 That's a slightly different time. 69 00:03:38,880 --> 00:03:40,960 It's off by a couple seconds. 70 00:03:40,960 --> 00:03:43,080 But we call methods on it like seconds. 71 00:03:43,080 --> 00:03:44,640 And we can also call Monday. 72 00:03:44,640 --> 00:03:46,840 I'll do that on the right object this time. 73 00:03:46,840 --> 00:03:47,840 Right. 74 00:03:47,840 --> 00:03:48,840 And it's false. 75 00:03:48,840 --> 00:03:53,800 So we've got t1 and t2 both have unique state, the current or the time at which they were 76 00:03:53,800 --> 00:03:54,800 created. 77 00:03:54,800 --> 00:03:57,200 But they've got a same set of behaviors. 78 00:03:57,200 --> 00:03:58,460 Now this is really important. 79 00:03:58,460 --> 00:04:02,080 So let's return to the diagrams with these examples. 80 00:04:02,080 --> 00:04:05,880 A class is a template or blueprint for creating objects. 81 00:04:05,880 --> 00:04:08,780 We instantiate objects from the class. 82 00:04:08,780 --> 00:04:13,320 Each object has common behavior, but different values for its state. 83 00:04:13,320 --> 00:04:16,899 So here we create a string object with the characters hello. 84 00:04:16,899 --> 00:04:21,560 All Ruby strings have common behavior, including reverse, upcase, and so on. 85 00:04:21,560 --> 00:04:24,640 We can also create a string object with the characters goodbye. 86 00:04:24,640 --> 00:04:28,479 Again, they're sharing the same behavior as all strings. 87 00:04:28,479 --> 00:04:31,280 And Mike showed us the same thing with the time class. 88 00:04:31,280 --> 00:04:37,000 Each time object stores its own internal time when it is created and shares the time related 89 00:04:37,000 --> 00:04:38,099 methods. 90 00:04:38,099 --> 00:04:43,239 So the takeaway is we create objects, sometimes called instances, from a class. 91 00:04:43,240 --> 00:04:48,560 And an object is some state variables and behavior, a set of methods. 92 00:04:48,560 --> 00:04:50,760 So you can think of it this way. 93 00:04:50,760 --> 00:04:53,820 State plus behavior equals object. 94 00:04:53,820 --> 00:04:55,560 So how do you know when to create a class? 95 00:04:55,560 --> 00:04:59,860 Well, when designing object-ornied programs, you try to identify the things that your program 96 00:04:59,860 --> 00:05:01,240 is going to deal with. 97 00:05:01,240 --> 00:05:04,920 And then you can define classes to then create those objects. 98 00:05:04,920 --> 00:05:07,280 Let's return to our movie example. 99 00:05:07,280 --> 00:05:09,160 So our code currently looks something like this. 100 00:05:09,160 --> 00:05:14,440 We have three different movie listings, and we've got different names and ranks for each 101 00:05:14,440 --> 00:05:15,440 movie. 102 00:05:15,440 --> 00:05:18,920 It seems fairly obvious that we need something to represent the concept of a movie in our 103 00:05:18,920 --> 00:05:19,920 program. 104 00:05:19,920 --> 00:05:21,580 So we'll need some movie objects. 105 00:05:21,580 --> 00:05:25,280 So it follows that we also need to create a movie class. 106 00:05:25,280 --> 00:05:26,280 That's right. 107 00:05:26,280 --> 00:05:30,040 We want to create a movie class that will create movie objects with the title and rank 108 00:05:30,040 --> 00:05:31,860 and some shared methods. 109 00:05:31,860 --> 00:05:33,440 So it will look like this. 110 00:05:33,440 --> 00:05:38,760 We'll get a movie object for Goonies with the rank of 10 and a movie object for Ghostbusters 111 00:05:38,760 --> 00:05:41,860 with the rank of 9, and they'll share some methods. 112 00:05:41,860 --> 00:05:43,840 So Mike, let's create a movie class. 113 00:05:43,840 --> 00:05:44,840 Sure. 114 00:05:44,840 --> 00:05:48,200 Let's just go ahead and clear out this file. 115 00:05:48,200 --> 00:05:49,760 And we're going to start fresh here. 116 00:05:49,760 --> 00:05:50,980 We want a movie class. 117 00:05:50,980 --> 00:05:51,980 So I'm going to start. 118 00:05:51,980 --> 00:05:55,420 We start with the keyword class, and then we want the name of the class. 119 00:05:55,420 --> 00:05:56,800 In this case, it's a movie. 120 00:05:56,800 --> 00:05:59,960 And class names always begin with an uppercase letter. 121 00:05:59,960 --> 00:06:01,340 And then we just use the keyword end. 122 00:06:01,340 --> 00:06:06,039 So that's the entire definition for a movie class, or at least the beginning of one. 123 00:06:06,039 --> 00:06:07,800 Now let's create some objects from that class. 124 00:06:07,800 --> 00:06:10,080 So let's say we have movie1. 125 00:06:10,080 --> 00:06:13,160 We can just call movie.new. 126 00:06:13,160 --> 00:06:18,060 And let's just go ahead and print out its object ID here. 127 00:06:18,060 --> 00:06:20,980 So when we run this, we've got the object ID for an object. 128 00:06:20,980 --> 00:06:25,300 So we just got an object in memory, a movie object in memory somewhere. 129 00:06:25,300 --> 00:06:26,300 So let's create another one now. 130 00:06:26,300 --> 00:06:27,300 We've got a class. 131 00:06:27,300 --> 00:06:29,000 We can create multiple objects. 132 00:06:29,000 --> 00:06:33,680 So we'll create another movie object, and we'll actually print out its object ID as 133 00:06:33,680 --> 00:06:34,680 well. 134 00:06:34,680 --> 00:06:38,000 I forgot to put a put us in front of there. 135 00:06:38,000 --> 00:06:41,560 If we run that, sure enough, we've got two movie objects. 136 00:06:41,560 --> 00:06:46,200 OK, so we know we've got two objects with two distinct IDs. 137 00:06:46,200 --> 00:06:48,360 Now we actually want to pass in some variables to these. 138 00:06:48,360 --> 00:06:50,960 We want to initialize some state of this movie. 139 00:06:50,960 --> 00:06:52,840 We know that a movie has a title. 140 00:06:52,840 --> 00:06:56,000 So when I create the movie, I want to pass in the title. 141 00:06:56,000 --> 00:06:57,960 In this case, it will be called Goonies. 142 00:06:57,960 --> 00:07:01,060 And we want to also pass in a rank like that. 143 00:07:01,060 --> 00:07:06,840 And then this movie will be something like Ghostbusters, and its rank will be 9. 144 00:07:06,840 --> 00:07:11,720 So if we run this now, we get this error, wrong number of arguments, 2 for 0. 145 00:07:11,720 --> 00:07:16,580 An interesting part of this is notice that new is being called, but this method initializes 146 00:07:16,580 --> 00:07:18,920 also being called on line 4. 147 00:07:18,920 --> 00:07:21,960 If we look at line 4, we're not calling initialize somewhere. 148 00:07:21,960 --> 00:07:23,440 So what's going on here? 149 00:07:23,440 --> 00:07:27,560 Well what happens is when you call new on a class, it kind of just sets up some initial 150 00:07:27,560 --> 00:07:31,720 memory for that object, but it doesn't initialize any of its state. 151 00:07:31,720 --> 00:07:33,520 Ruby doesn't know how to do that. 152 00:07:33,520 --> 00:07:38,760 Instead, new call turns around, and it calls a method called initialize, and it's going 153 00:07:38,760 --> 00:07:41,760 to pass in those parameters to initialize. 154 00:07:41,760 --> 00:07:47,680 So inside of our movie class, we need to define the method called initialize. 155 00:07:47,680 --> 00:07:51,560 It's going to take two parameters, our title and our rank, just like that. 156 00:07:51,560 --> 00:08:01,840 And inside of initialize, I'll just print out created a movie object with title, title, 157 00:08:01,840 --> 00:08:06,880 and rank, rank, just like that. 158 00:08:06,880 --> 00:08:07,880 Save it off. 159 00:08:07,880 --> 00:08:10,440 Now if we run it, we see that that string is being printed. 160 00:08:10,440 --> 00:08:13,400 So the initialize method is being called, and we see that our parameters are getting 161 00:08:13,400 --> 00:08:14,400 passed in. 162 00:08:14,400 --> 00:08:16,040 We've got Goonies and a rank of 10. 163 00:08:16,040 --> 00:08:17,720 OK, this is cool. 164 00:08:17,720 --> 00:08:21,840 So now we have our two movie objects, but they can't really do much yet. 165 00:08:21,840 --> 00:08:24,040 Right, so let's add some behavior. 166 00:08:24,040 --> 00:08:25,680 I only need one movie object for right now. 167 00:08:25,680 --> 00:08:27,660 I'm just going to take that one away. 168 00:08:27,660 --> 00:08:29,280 So let's say we want to do something like this. 169 00:08:29,280 --> 00:08:33,560 We want to call putS, and then we're going to take our movie one object, and let's say 170 00:08:33,560 --> 00:08:37,800 we want to call a method called listing that's going to return a string, and then we can 171 00:08:37,800 --> 00:08:39,900 print it out to the console. 172 00:08:39,900 --> 00:08:44,680 Now notice here that we want to call listing on this particular object. 173 00:08:44,680 --> 00:08:46,220 So we have a receiver here. 174 00:08:46,220 --> 00:08:47,880 This is often called an instance method. 175 00:08:47,880 --> 00:08:51,540 We want to call that method on an instance of the movie class. 176 00:08:51,540 --> 00:08:54,320 So let's go ahead and define that method up in our class. 177 00:08:54,320 --> 00:08:56,760 We'll just define listing like that. 178 00:08:56,760 --> 00:09:00,640 And inside the listing, I want to return a string, and I want the string to be title 179 00:09:00,640 --> 00:09:04,360 has a rank of rank, just like that. 180 00:09:04,360 --> 00:09:06,400 Let's go ahead and run it. 181 00:09:06,400 --> 00:09:07,400 Oh. 182 00:09:07,400 --> 00:09:11,760 Yeah, we get our initial string when we created the movie object up in the initialize method, 183 00:09:11,760 --> 00:09:18,200 but then we get this undefined local variable or method title, which is on line seven. 184 00:09:18,200 --> 00:09:19,200 So what's going on here? 185 00:09:19,200 --> 00:09:23,640 Well, inside of this method, remember, methods are like little black boxes. 186 00:09:23,640 --> 00:09:28,480 What Ruby's trying to do here is find a local variable called title and a local variable 187 00:09:28,480 --> 00:09:31,480 called rank, but it's not finding those. 188 00:09:31,480 --> 00:09:35,560 Remember when we called initialize, these parameters were passed in. 189 00:09:35,560 --> 00:09:40,400 Title and rank up here were set to whatever we passed in, but then when the initialize 190 00:09:40,400 --> 00:09:44,740 method ends, those variables then evaporate. 191 00:09:44,740 --> 00:09:49,120 So we need some way to hold onto these variables inside of initialize so then we can reference 192 00:09:49,120 --> 00:09:51,120 them later inside of listing. 193 00:09:51,120 --> 00:09:55,000 And the way we do that is to transfer these variables over to what's called an instance 194 00:09:55,000 --> 00:09:56,000 variable. 195 00:09:56,000 --> 00:09:59,760 And an instance variable begins with an at sign, and I'm going to call the first one 196 00:09:59,760 --> 00:10:03,480 title, although it doesn't have to match the incoming parameter, and I'm going to assign 197 00:10:03,480 --> 00:10:07,040 to that the local variable title. 198 00:10:07,040 --> 00:10:10,240 In the same way, I'm going to have an instance variable called rank, and I'm going to transfer 199 00:10:10,240 --> 00:10:14,200 over the value of the rank parameter that's coming in. 200 00:10:14,200 --> 00:10:20,960 And while I'm at it, I'll go ahead and call capitalize on this title, capitalize on the 201 00:10:20,960 --> 00:10:26,520 title there so that the title instance variable always has the capitalized form of the title 202 00:10:26,520 --> 00:10:27,520 there. 203 00:10:27,520 --> 00:10:31,600 So what's cool about instance variables here is they live for the life of the object, and 204 00:10:31,600 --> 00:10:35,640 instance methods can then access those instance variables. 205 00:10:35,640 --> 00:10:40,120 So down in listing now, we can change this to at title, that's the name of the instance 206 00:10:40,120 --> 00:10:43,920 variable, and we can change this to at rank. 207 00:10:43,920 --> 00:10:46,920 Now when we run the program, it works as we would expect. 208 00:10:46,920 --> 00:10:49,600 Goonies has a rank of 10. 209 00:10:49,600 --> 00:10:53,320 And just to clean this up a little bit more, we can actually remove this put s line because 210 00:10:53,320 --> 00:10:55,840 we know that this is getting initialized the way we want. 211 00:10:55,840 --> 00:10:57,600 All the instance variables are set. 212 00:10:57,600 --> 00:11:03,120 So our state of our object is getting set up and initialized for us, and then our instance 213 00:11:03,120 --> 00:11:06,720 methods can turn around and use that state. 214 00:11:06,720 --> 00:11:10,320 Now we should probably point out that the instance variable name does not need to match 215 00:11:10,320 --> 00:11:12,080 the parameter name. 216 00:11:12,080 --> 00:11:13,200 That's a really good point. 217 00:11:13,200 --> 00:11:15,120 These are two separate variables. 218 00:11:15,120 --> 00:11:18,580 At rank is an instance variable, rank is a local variable. 219 00:11:18,580 --> 00:11:22,560 So we could call this, for example, the rank if we wanted to. 220 00:11:22,560 --> 00:11:25,800 And then we just have to make sure that wherever we reference that, we'd have to change it 221 00:11:25,800 --> 00:11:28,880 to the rank. 222 00:11:28,880 --> 00:11:30,480 Let's go ahead and add in another movie here. 223 00:11:30,480 --> 00:11:33,020 I took it away earlier, but we want a couple movies in here. 224 00:11:33,020 --> 00:11:35,160 So movie two is going to be. 225 00:11:35,160 --> 00:11:36,160 Ghostbusters. 226 00:11:36,160 --> 00:11:37,160 Ghostbusters back. 227 00:11:37,160 --> 00:11:41,839 Ghostbusters, and we'll have a rank of nine. 228 00:11:41,839 --> 00:11:46,439 And then we can call the method movie two dot listing because it's an instance method 229 00:11:46,439 --> 00:11:48,040 on the object. 230 00:11:48,040 --> 00:11:50,600 And now we've got two movies. 231 00:11:50,600 --> 00:11:54,199 Now remember that methods can take a default parameter value. 232 00:11:54,199 --> 00:11:57,360 So we could actually change our rank to have a default of zero. 233 00:11:57,360 --> 00:11:59,400 Yeah, let's do that with a third movie. 234 00:11:59,400 --> 00:12:01,140 Let's say we have movie three. 235 00:12:01,140 --> 00:12:02,140 It's going to be. 236 00:12:02,140 --> 00:12:03,140 Goldfinger. 237 00:12:03,140 --> 00:12:05,100 Goldfinger. 238 00:12:05,100 --> 00:12:07,800 And we don't want to pass in an initial rank here. 239 00:12:07,800 --> 00:12:11,080 So this isn't going to work if we run it because it's a required parameter. 240 00:12:11,080 --> 00:12:14,000 But we know that initialize is just like any other method. 241 00:12:14,000 --> 00:12:15,940 It can take default parameter value. 242 00:12:15,940 --> 00:12:19,320 So we'll just change that to a zero just like that. 243 00:12:19,320 --> 00:12:24,440 And I suppose I should print out Goldfinger's listing at the bottom. 244 00:12:24,440 --> 00:12:26,000 And there we go. 245 00:12:26,000 --> 00:12:27,620 Three movies. 246 00:12:27,620 --> 00:12:29,760 So we need a way to print out multiple movies. 247 00:12:29,760 --> 00:12:31,600 We're going to do that over and over and over. 248 00:12:31,600 --> 00:12:32,600 Yeah. 249 00:12:32,600 --> 00:12:36,820 It's kind of inconvenient if we always have to call this listing method to print out movies. 250 00:12:36,820 --> 00:12:40,560 So what we'd rather do is instead of calling listing here, we'd rather just pass the entire 251 00:12:40,560 --> 00:12:43,200 movie object into the put s method. 252 00:12:43,200 --> 00:12:44,200 Right. 253 00:12:44,200 --> 00:12:45,600 So just put s movie one. 254 00:12:45,600 --> 00:12:46,840 What could be simpler than that? 255 00:12:46,840 --> 00:12:47,840 Right. 256 00:12:47,840 --> 00:12:53,000 So if we run this now, we actually get this kind of not very convenient output. 257 00:12:53,000 --> 00:12:56,900 We just get the name of the class movie and then some hex value here. 258 00:12:56,900 --> 00:13:01,120 So what's happening is when you pass an object to put s, put s is going to try to convert 259 00:13:01,120 --> 00:13:04,000 that object into its string representation. 260 00:13:04,000 --> 00:13:08,200 And the way it does that is to call this method called to underscore s on an object. 261 00:13:08,200 --> 00:13:09,700 And all objects have these. 262 00:13:09,700 --> 00:13:14,300 It just happens to be that the default implementation of that method isn't very helpful. 263 00:13:14,300 --> 00:13:15,300 So we can change that. 264 00:13:15,300 --> 00:13:16,480 We've got this listing up here. 265 00:13:16,480 --> 00:13:18,000 It returns a string. 266 00:13:18,000 --> 00:13:20,120 To s is expected to return a string. 267 00:13:20,120 --> 00:13:22,920 So we can just change this to to underscore s. 268 00:13:22,920 --> 00:13:27,820 This method will get automatically called when put s tries to convert this object over 269 00:13:27,820 --> 00:13:28,820 to a string. 270 00:13:28,820 --> 00:13:30,080 So let's go ahead and run that. 271 00:13:30,080 --> 00:13:34,840 And now we get the string that we expect when we print out the entire movie object. 272 00:13:34,840 --> 00:13:35,840 All right. 273 00:13:35,840 --> 00:13:39,600 So this is pretty cool because we can now create movie objects. 274 00:13:39,600 --> 00:13:42,820 And then when we talk to each movie, we can just use one variable. 275 00:13:42,820 --> 00:13:47,320 We can use movie one refers to the whole object, movie two, and movie three. 276 00:13:47,320 --> 00:13:50,400 We also have a place where we can now add behavior. 277 00:13:50,400 --> 00:13:53,360 Like we could thumbs up a movie or thumbs down a movie. 278 00:13:53,360 --> 00:13:54,360 Yeah. 279 00:13:54,360 --> 00:13:56,520 When we thumbs up a movie, we just increase its rank by one. 280 00:13:56,520 --> 00:13:59,640 And if we thumbs down a movie, we decrease its rank by one. 281 00:13:59,640 --> 00:14:00,640 Right. 282 00:14:00,640 --> 00:14:03,319 So we need some way to change the internal state of a movie. 283 00:14:03,319 --> 00:14:05,800 It's at rank instance variable. 284 00:14:05,800 --> 00:14:10,920 That kind of reminds me of an old programming maxim called tell don't ask. 285 00:14:10,920 --> 00:14:15,400 So rather than ask an object for its state, then make some decision on its behalf and 286 00:14:15,400 --> 00:14:17,240 then tell the object what to do. 287 00:14:17,240 --> 00:14:21,400 We prefer to tell the object what to do straight away and then let it figure out how to do 288 00:14:21,400 --> 00:14:22,400 that. 289 00:14:22,400 --> 00:14:25,680 And we tell an object to do something by calling a method. 290 00:14:25,680 --> 00:14:27,120 So we need to write two methods. 291 00:14:27,120 --> 00:14:28,800 We need a thumbs up and a thumbs down. 292 00:14:28,800 --> 00:14:29,800 Yeah. 293 00:14:29,800 --> 00:14:31,040 Let's start with the thumbs up method. 294 00:14:31,040 --> 00:14:33,880 So we want to be able to thumbs up a particular object. 295 00:14:33,880 --> 00:14:36,160 So we're going to need an instance method to help us do that. 296 00:14:36,160 --> 00:14:38,040 So I'm just going to define an instance method up here. 297 00:14:38,040 --> 00:14:42,359 I'm going to call it thumbs up, just like that. 298 00:14:42,359 --> 00:14:47,120 And all this method is going to do is we want to assign a new rank and it's going to be 299 00:14:47,120 --> 00:14:49,359 the existing rank plus one. 300 00:14:49,359 --> 00:14:55,880 So we're just going to add one to the rank and then down for Goonies, for example, and 301 00:14:55,880 --> 00:14:58,439 call movie one dot thumbs up. 302 00:14:58,440 --> 00:14:59,820 It's going to start at a rank of 10. 303 00:14:59,820 --> 00:15:00,820 That's its initial rank. 304 00:15:00,820 --> 00:15:03,680 Now, if we run it, it now has a rank of 11. 305 00:15:03,680 --> 00:15:06,200 So now we need a way to thumbs down in movie objects. 306 00:15:06,200 --> 00:15:07,800 We need another instance method for that. 307 00:15:07,800 --> 00:15:11,240 We're going to define thumbs down. 308 00:15:11,240 --> 00:15:16,560 The implementation of that will be to take the existing rank and subtract one. 309 00:15:16,560 --> 00:15:17,560 And then let's see. 310 00:15:17,560 --> 00:15:18,560 Yeah, let's thumbs down Ghostbusters. 311 00:15:18,560 --> 00:15:22,320 You know, that was kind of scary in its day. 312 00:15:22,320 --> 00:15:23,920 Thumbs down to Ghostbusters. 313 00:15:23,920 --> 00:15:25,200 We're so sorry. 314 00:15:25,200 --> 00:15:26,320 Right. 315 00:15:26,320 --> 00:15:31,000 So it starts at nine and if we run it, now it has a rank of eight. 316 00:15:31,000 --> 00:15:33,200 So now the object has state and behavior. 317 00:15:33,200 --> 00:15:34,200 Right, right. 318 00:15:34,200 --> 00:15:38,540 We can thumbs up or thumbs down it and change the internal state, the rank. 319 00:15:38,540 --> 00:15:43,160 One little thing I'd like to do here, though, is we saw when we're thumbs up and we just 320 00:15:43,160 --> 00:15:45,160 take the initial rank and add one. 321 00:15:45,160 --> 00:15:47,400 There's a shortcut we can use in Ruby to do this. 322 00:15:47,400 --> 00:15:49,960 And it's just this plus equals. 323 00:15:49,960 --> 00:15:53,960 Plus equals is going to take the existing value of rank, add one, and then assign it 324 00:15:53,960 --> 00:15:55,840 back to the rank instance variable. 325 00:15:55,840 --> 00:16:00,080 In the same way for thumbs down, we can do minus equals and that'll subtract one from 326 00:16:00,080 --> 00:16:01,480 the existing value. 327 00:16:01,480 --> 00:16:03,480 And we get exactly the same thing. 328 00:16:03,480 --> 00:16:06,560 Okay, so here's a slow motion replay. 329 00:16:06,560 --> 00:16:12,160 When we call the new method like this, we get a new movie object with its instance variables 330 00:16:12,160 --> 00:16:13,520 already filled in. 331 00:16:13,520 --> 00:16:15,760 Goonies and a rank of 10. 332 00:16:15,760 --> 00:16:18,600 Then to change the movie state, we call methods. 333 00:16:18,600 --> 00:16:22,840 So by calling thumbs down, the rank changes from 10 to nine. 334 00:16:22,840 --> 00:16:26,800 Calling it again changes the rank from nine to eight. 335 00:16:26,800 --> 00:16:31,320 And here's another movie object with different values for its instance variables. 336 00:16:31,320 --> 00:16:36,000 We call thumbs up on it and it changes the rank from nine to 10. 337 00:16:36,000 --> 00:16:39,680 And then we call it again and it changes the rank from 10 to 11. 338 00:16:39,680 --> 00:16:43,360 So now we have two objects with two distinct states. 339 00:16:43,360 --> 00:16:46,520 We can think of each object like another black box. 340 00:16:46,520 --> 00:16:50,600 From the outside, you interface with it by calling methods and you don't actually know 341 00:16:50,600 --> 00:16:52,320 what goes on inside. 342 00:16:52,320 --> 00:16:55,040 This is the beauty of objects. 343 00:16:55,040 --> 00:16:57,120 So just a quick vocab review here. 344 00:16:57,120 --> 00:17:02,500 We set up our state in the initialized method by setting instance variables. 345 00:17:02,500 --> 00:17:06,760 And then our behavior is expressed as instance methods inside of the class. 346 00:17:06,760 --> 00:17:11,440 Then we use the class to create an object and then we can call those instance methods 347 00:17:11,440 --> 00:17:12,440 on the object. 348 00:17:12,440 --> 00:17:16,080 All right, so you're ready to give this a go for yourself? 349 00:17:16,080 --> 00:17:18,880 In the exercise, you're going to write your own player class. 350 00:17:18,880 --> 00:17:22,840 You're also going to write some methods to change up the state of your players, very 351 00:17:22,840 --> 00:17:26,000 similar to how we change the state of our movies. 352 00:17:26,000 --> 00:17:30,160 And now that we have a start on classes, in the next section, we'll look at another aspect 353 00:17:30,160 --> 00:17:31,800 of classes called attributes. 354 00:17:31,800 --> 00:17:32,800 We'll see you then. 355 00:17:32,800 --> 00:17:49,399 See you then. 30555

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