All language subtitles for pragstudio-ruby-16-symbols-structs (Transcribed on 24-Apr-2023 20-58-33)

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:05,320 Now the game is coming along quite nicely. 2 00:00:05,320 --> 00:00:10,080 You have multiple players, you have multiple numbers of rounds, random wooting and blamming 3 00:00:10,080 --> 00:00:12,920 with a die, and you have some basic stats. 4 00:00:12,920 --> 00:00:16,760 But it's time to introduce a new dimension to the game, a treasure hunt. 5 00:00:16,760 --> 00:00:21,380 The idea is that the game has treasures, and each treasure has a name and a point value, 6 00:00:21,380 --> 00:00:23,640 and we put them in a treasure trove. 7 00:00:23,640 --> 00:00:27,260 Then when a player takes a turn, he finds a random treasure. 8 00:00:27,260 --> 00:00:29,520 But of course we'll leave that for you in the exercise. 9 00:00:29,520 --> 00:00:32,040 We're going to do something similar with the movie app. 10 00:00:32,040 --> 00:00:36,000 Instead of treasures, we'll have snacks with a name and a card value. 11 00:00:36,000 --> 00:00:37,840 We'll put them in a snack bar. 12 00:00:37,840 --> 00:00:41,600 Then when a movie is played, a random snack gets consumed. 13 00:00:41,600 --> 00:00:45,480 This gives us an opportunity to revisit symbols and also learn about structs. 14 00:00:45,480 --> 00:00:49,800 We'll model both the treasure and the snack as a struct, and we'll see some symbols along 15 00:00:49,800 --> 00:00:50,800 the way. 16 00:00:50,800 --> 00:00:53,120 Now, we've actually seen symbols before. 17 00:00:53,120 --> 00:00:54,120 Let's revisit them. 18 00:00:54,120 --> 00:00:56,440 Now, symbols start with a colon. 19 00:00:56,440 --> 00:00:58,540 Here health is a symbol. 20 00:00:58,540 --> 00:01:02,200 When you see a symbol, think name, because that's all a symbol really is. 21 00:01:02,200 --> 00:01:06,060 It's a convenient way to name or identify something in our code. 22 00:01:06,060 --> 00:01:08,360 For example, here we're naming an attribute. 23 00:01:08,360 --> 00:01:14,060 We want an attribute named health, or in this case, an attribute named name. 24 00:01:14,060 --> 00:01:18,680 Or perhaps we have a method called find with an option named all. 25 00:01:18,680 --> 00:01:23,240 Or we might want to refer to colors by name, a color named red. 26 00:01:23,240 --> 00:01:28,080 Or let's say we had some sort of support ticket system where we wanted to represent the statuses 27 00:01:28,080 --> 00:01:30,300 as open, closed, or pending. 28 00:01:30,300 --> 00:01:32,720 We could use an array of symbols for that. 29 00:01:32,720 --> 00:01:36,280 So symbols are just a way for us to name things in our code. 30 00:01:36,280 --> 00:01:39,759 So let's start by experimenting with symbols in IRB. 31 00:01:39,759 --> 00:01:41,800 So as Nicole said, symbols start with a colon. 32 00:01:41,800 --> 00:01:44,680 So here's a symbol for the color green. 33 00:01:44,680 --> 00:01:49,039 And if we look at the class of that thing, well, it's a symbol. 34 00:01:49,039 --> 00:01:52,920 Now this is different from a single or double quoted string green. 35 00:01:52,920 --> 00:01:55,360 We look at its class, well, it's a string. 36 00:01:55,360 --> 00:01:58,840 So let's look at the object ID for our symbol, green. 37 00:01:58,840 --> 00:02:00,640 All right, we get that number. 38 00:02:00,640 --> 00:02:05,320 If we just use the same symbol again, they're exactly the same object ID. 39 00:02:05,320 --> 00:02:08,440 Ruby makes sure there's only one value for the green symbol. 40 00:02:08,440 --> 00:02:09,919 Green is green is green. 41 00:02:09,919 --> 00:02:14,120 In other words, a particular symbol refers to the same symbol object. 42 00:02:14,120 --> 00:02:20,420 In contrast to that, if we look at our string green, we look at object ID, we get that number. 43 00:02:20,420 --> 00:02:24,700 If we create another string object here, well, we're going to get a different string object. 44 00:02:24,700 --> 00:02:27,500 So strings are separate objects. 45 00:02:27,500 --> 00:02:30,420 The green symbol is always the same object. 46 00:02:30,420 --> 00:02:32,160 And these are two very different types in Ruby. 47 00:02:32,160 --> 00:02:37,000 If we look at green, the string, see if it's equal to green, the symbol. 48 00:02:37,000 --> 00:02:38,000 No it's not. 49 00:02:38,000 --> 00:02:39,519 Again, they're different objects. 50 00:02:39,519 --> 00:02:42,120 But it's fairly easy to convert from one to another. 51 00:02:42,120 --> 00:02:47,760 So if we had the green symbol, we could convert it to a string simply by calling the to underscore 52 00:02:47,760 --> 00:02:49,899 method, just like that. 53 00:02:49,900 --> 00:02:56,140 Or if we had the green string, we could call the to symbol method and we get the symbol 54 00:02:56,140 --> 00:02:57,220 for that. 55 00:02:57,220 --> 00:03:01,460 So symbols are just used to identify or stand for something in our code. 56 00:03:01,460 --> 00:03:03,520 You could think of it as a constant string if you want to. 57 00:03:03,520 --> 00:03:05,540 It's actually a different type than that. 58 00:03:05,540 --> 00:03:08,540 But we use them just to identify or stand for something. 59 00:03:08,540 --> 00:03:14,800 In fact, if you look at the instance methods on the symbol class, you get something like 60 00:03:14,800 --> 00:03:15,800 that. 61 00:03:15,800 --> 00:03:18,180 Notice that we don't have all the methods that we had for a string. 62 00:03:18,180 --> 00:03:23,700 If we look at strings instance methods, well it has a whole bunch more because you want 63 00:03:23,700 --> 00:03:26,980 to do things like capitalize and reverse and get the length of. 64 00:03:26,980 --> 00:03:29,660 You want to do some text processing with the string. 65 00:03:29,660 --> 00:03:31,540 Now don't overthink symbols. 66 00:03:31,540 --> 00:03:33,260 Here's the important distinction. 67 00:03:33,260 --> 00:03:37,460 You use symbols when you want to name something like an attribute or an option. 68 00:03:37,460 --> 00:03:41,860 And you use strings when you want string-like behavior or you want to do some text processing. 69 00:03:41,860 --> 00:03:45,940 Yeah, and if you're still unsure about symbols, just go with the flow for now. 70 00:03:45,940 --> 00:03:49,060 You've seen them for a while, using them becomes a lot more natural. 71 00:03:49,060 --> 00:03:52,140 So let's look at a more practical example of symbols. 72 00:03:52,140 --> 00:03:57,620 Now we want to model a snack, and it just has two attributes, name and carb count. 73 00:03:57,620 --> 00:04:02,620 Yeah, we could write a class that we're used to, say something like snack, and then, oh 74 00:04:02,620 --> 00:04:08,540 I don't know, we'll just use attribute reader and we could give it name and carbs because 75 00:04:08,540 --> 00:04:11,700 you said those are the only attributes we really need, right? 76 00:04:11,700 --> 00:04:17,420 We need to initialize it, initialize, we pass in name and we pass in carbs, and then we'd 77 00:04:17,420 --> 00:04:22,099 have to assign those to instance variables, of course. 78 00:04:22,099 --> 00:04:24,659 Carbs equals carbs, something like that. 79 00:04:24,659 --> 00:04:26,780 Then we could create snacks from that class. 80 00:04:26,780 --> 00:04:27,780 We've seen how to do that. 81 00:04:27,780 --> 00:04:29,140 Let's say our snack is popcorn. 82 00:04:29,140 --> 00:04:30,140 Popcorn. 83 00:04:30,140 --> 00:04:33,180 Mmm, popcorn. 84 00:04:33,180 --> 00:04:37,820 And the name of that would be popcorn, that's the name we pass in, and let's say it's got 85 00:04:37,820 --> 00:04:40,919 a carb value of 20. 86 00:04:40,920 --> 00:04:46,500 And then we could print out its name, because we have an attribute reader for that, and 87 00:04:46,500 --> 00:04:51,020 we could print out its carbs, like that. 88 00:04:51,020 --> 00:04:52,780 Let's just see if that works. 89 00:04:52,780 --> 00:04:56,340 Sure enough, we've got popcorn with a carb value of 20. 90 00:04:56,340 --> 00:04:59,980 So we could definitely do that, but I don't know what other behavior a snack's actually 91 00:04:59,980 --> 00:05:01,340 going to have. 92 00:05:01,340 --> 00:05:05,940 So it gives us an opportunity to talk about something different in Ruby, which is a struct. 93 00:05:05,940 --> 00:05:08,820 And we can do the same thing that we have this class. 94 00:05:08,820 --> 00:05:12,980 Instead of writing an explicit class, we can do this in a struct. 95 00:05:12,980 --> 00:05:20,659 We'll just comment out our class, like that, and we'll assign to a constant called snack, 96 00:05:20,659 --> 00:05:26,300 and then we say struct.new, and then we have to give it the names of the attributes we 97 00:05:26,300 --> 00:05:29,260 want the struct to have. 98 00:05:29,260 --> 00:05:33,020 Name and carbs, just like what we did with AtroReader up here. 99 00:05:33,020 --> 00:05:34,500 It's got name and carbs. 100 00:05:34,500 --> 00:05:36,360 This is struct.new. 101 00:05:36,360 --> 00:05:40,620 So a struct is just a collection of attributes, and it saves us from having to write an explicit 102 00:05:40,620 --> 00:05:41,620 class. 103 00:05:41,620 --> 00:05:46,520 In fact, a struct generates a class object, just like a class definition did. 104 00:05:46,520 --> 00:05:50,380 So we've got this constant snack, it starts with an uppercase letter. 105 00:05:50,380 --> 00:05:55,420 It's as if we defined a class called snack, and we can use it just like we did before. 106 00:05:55,420 --> 00:05:58,460 We can call snack.new, and we get the same thing. 107 00:05:58,460 --> 00:06:01,180 We get popcorn and 20. 108 00:06:01,180 --> 00:06:05,260 So we're using symbols here to denote the attributes that we want, because we're just 109 00:06:05,260 --> 00:06:10,260 having an attribute named name or an attribute named carbs, and the struct will take care 110 00:06:10,260 --> 00:06:12,780 of creating the accessor methods for that. 111 00:06:12,780 --> 00:06:17,980 So we can get popcorn.name, for example, just as we would if we had attributes inside of 112 00:06:17,980 --> 00:06:19,260 a class. 113 00:06:19,260 --> 00:06:20,500 So should we create another snack? 114 00:06:20,500 --> 00:06:22,500 Yeah, let's do that. 115 00:06:22,500 --> 00:06:23,860 Let's create candy. 116 00:06:23,860 --> 00:06:24,860 Candy. 117 00:06:24,860 --> 00:06:28,940 All right, we use our constant snack, new. 118 00:06:28,940 --> 00:06:30,940 The name of that is going to be just candy. 119 00:06:30,940 --> 00:06:34,940 Okay, now we should take a moment here to point out that we are not the authority on 120 00:06:34,940 --> 00:06:38,060 the nutritional value of snacks. 121 00:06:38,060 --> 00:06:40,060 So let's make its carb value 15. 122 00:06:40,060 --> 00:06:43,940 I don't really know if that's more or less than the carb value of popcorn. 123 00:06:43,940 --> 00:06:44,940 Let's just say it's 15. 124 00:06:44,940 --> 00:06:45,940 That sounds really good. 125 00:06:45,940 --> 00:06:46,940 Right, right. 126 00:06:46,940 --> 00:06:48,460 I really wish Snickers were 15. 127 00:06:48,460 --> 00:06:56,860 Okay, so we've got some candy, so we can print out candy has a name and candy has carbs. 128 00:06:56,860 --> 00:07:01,240 So now we've got two snack objects, popcorn and candy. 129 00:07:01,240 --> 00:07:06,540 Now a snack may grow up to be a class someday, but for now a struct is all we need. 130 00:07:06,540 --> 00:07:10,020 Yeah, we just have these attributes, so I'm just going to remove the class. 131 00:07:10,020 --> 00:07:13,180 We could convert that to a class later if we wanted to, but let's just leave it a 132 00:07:13,180 --> 00:07:14,180 struct for now. 133 00:07:14,180 --> 00:07:15,820 Okay, this is cool. 134 00:07:15,820 --> 00:07:17,540 Now we know how to create snacks. 135 00:07:17,540 --> 00:07:18,540 Let's put them in a snack bar. 136 00:07:18,540 --> 00:07:20,580 Hmm, a snack bar. 137 00:07:20,580 --> 00:07:22,100 So how are we going to model that? 138 00:07:22,100 --> 00:07:24,420 Well, we don't need multiple objects. 139 00:07:24,420 --> 00:07:27,460 There's only going to be one snack bar in our entire program. 140 00:07:27,460 --> 00:07:28,460 Right. 141 00:07:28,460 --> 00:07:30,360 And we saw we did the same thing with reviewer earlier. 142 00:07:30,360 --> 00:07:32,880 So let's make the snack bar a module. 143 00:07:32,880 --> 00:07:35,740 So we're in our snackbar.rb file where we have this struct defined. 144 00:07:35,740 --> 00:07:37,780 I'm just going to leave that at the top. 145 00:07:37,780 --> 00:07:39,040 We want a new module. 146 00:07:39,040 --> 00:07:43,820 The module is going to be called snackbar. 147 00:07:43,820 --> 00:07:48,780 And we saw that we can put methods inside of modules or module methods inside of modules 148 00:07:48,780 --> 00:07:49,780 earlier. 149 00:07:49,780 --> 00:07:52,140 We can also put other things inside of modules. 150 00:07:52,140 --> 00:07:56,100 For example, our snack bar, we kind of want an array of snacks that the snack bar has 151 00:07:56,100 --> 00:07:57,140 inside of it. 152 00:07:57,140 --> 00:07:58,860 So let's do that as a constant. 153 00:07:58,860 --> 00:08:03,700 And a constant in Ruby is all uppercase characters, which can have a snacks constant. 154 00:08:03,700 --> 00:08:06,060 And it's just going to be an array. 155 00:08:06,060 --> 00:08:07,700 And what do we want inside of the snacks array? 156 00:08:07,700 --> 00:08:08,900 Well, we want some snacks. 157 00:08:08,900 --> 00:08:11,380 So we can use the struct that we defined earlier. 158 00:08:11,380 --> 00:08:12,860 So I'm going to have a snack. 159 00:08:12,860 --> 00:08:15,140 I'm going to call snack.new. 160 00:08:15,140 --> 00:08:17,900 And we'll put, let's put popcorn in our snack bar. 161 00:08:17,900 --> 00:08:18,900 Okay, popcorn. 162 00:08:18,900 --> 00:08:21,860 Now, we could do it like this with a string popcorn. 163 00:08:21,860 --> 00:08:23,900 What's the nutritional value of popcorn again? 164 00:08:23,900 --> 00:08:24,900 20. 165 00:08:24,900 --> 00:08:25,900 Okay. 166 00:08:25,900 --> 00:08:30,299 So we really only can be referencing the names of these snacks inside of our program. 167 00:08:30,299 --> 00:08:34,539 So let's just go ahead and do these as symbols because they're just something we're going 168 00:08:34,539 --> 00:08:35,539 to name. 169 00:08:35,539 --> 00:08:36,539 All right. 170 00:08:36,539 --> 00:08:38,720 We're going to print them out to the screen, but that's going to be easy enough. 171 00:08:38,720 --> 00:08:40,459 So we'll just leave them as symbols for now. 172 00:08:40,459 --> 00:08:41,459 So we've got popcorn. 173 00:08:41,459 --> 00:08:43,459 Give me a few other snacks here. 174 00:08:43,459 --> 00:08:44,459 Oh, let's see. 175 00:08:44,459 --> 00:08:45,900 We should put candy back in it. 176 00:08:45,900 --> 00:08:47,220 Let's do 15. 177 00:08:47,220 --> 00:08:49,220 Candy 15. 178 00:08:49,220 --> 00:08:50,220 Let's do nachos. 179 00:08:50,220 --> 00:08:51,220 Ooh, nachos. 180 00:08:51,220 --> 00:08:52,220 All right. 181 00:08:52,220 --> 00:08:55,220 And then, oh, let's put some pretzels in there. 182 00:08:55,220 --> 00:08:56,660 Let's put some pretzels in. 183 00:08:56,660 --> 00:08:57,940 Nachos was how many? 184 00:08:57,940 --> 00:08:58,940 Oh, nachos. 185 00:08:58,940 --> 00:09:01,580 Those are kind of expensive, 40. 186 00:09:01,580 --> 00:09:02,580 Wow. 187 00:09:02,580 --> 00:09:05,220 Well, look at all that cheese. 188 00:09:05,220 --> 00:09:07,420 Let's do a pretzel for 10. 189 00:09:07,420 --> 00:09:08,420 Pretzel for 10. 190 00:09:08,420 --> 00:09:09,420 Okay. 191 00:09:09,420 --> 00:09:10,540 And then we probably need something to drink. 192 00:09:10,540 --> 00:09:13,580 So let's do, well, I call it pop. 193 00:09:13,580 --> 00:09:16,060 People in other parts of the country call it soda. 194 00:09:16,060 --> 00:09:17,180 Yeah, soda. 195 00:09:17,180 --> 00:09:19,260 So it's low, like five. 196 00:09:19,260 --> 00:09:20,260 Right. 197 00:09:20,260 --> 00:09:21,260 Soda's cheap. 198 00:09:21,260 --> 00:09:22,260 Okay. 199 00:09:22,260 --> 00:09:23,460 So we've got our array of snacks now. 200 00:09:23,460 --> 00:09:25,380 And it's inside of this module. 201 00:09:25,380 --> 00:09:26,380 Right. 202 00:09:26,380 --> 00:09:31,020 I left the snack struct outside of here because we might want to access that later outside 203 00:09:31,020 --> 00:09:33,900 of the module, give or take whether you want to put it inside of the module. 204 00:09:33,900 --> 00:09:35,900 But we're going to leave it outside for right now. 205 00:09:35,900 --> 00:09:39,500 So just given that module, how would we print out all of our snacks? 206 00:09:39,500 --> 00:09:41,340 Well, we'd use putS. 207 00:09:41,340 --> 00:09:44,300 We use our module name, snackbar. 208 00:09:44,300 --> 00:09:49,020 And then to get access to this constant inside of the module, it's sort of namespaced inside 209 00:09:49,020 --> 00:09:50,020 of there. 210 00:09:50,020 --> 00:09:54,460 To get access to it, we use the scope resolution operator, that's a double colon, and then 211 00:09:54,460 --> 00:09:56,860 we just give it snacks, which is the constant. 212 00:09:56,860 --> 00:10:00,780 So we're saying we want this constant inside of that module. 213 00:10:00,780 --> 00:10:03,220 So we run that and we get a printout. 214 00:10:03,220 --> 00:10:04,340 We've got all of our snacks. 215 00:10:04,340 --> 00:10:06,460 You see the name is a symbol, naming it. 216 00:10:06,460 --> 00:10:07,500 We've got the car values. 217 00:10:07,500 --> 00:10:11,180 So we've got part of our snack bar set up here. 218 00:10:11,180 --> 00:10:15,860 Now every time we play a movie, we want to select a random snack from the snack bar. 219 00:10:15,860 --> 00:10:16,860 Oh, right. 220 00:10:16,860 --> 00:10:20,640 Because we need some way to get one of these random things out of the array. 221 00:10:20,640 --> 00:10:23,580 So we saw that we can define module methods earlier. 222 00:10:23,580 --> 00:10:25,060 Let's just create a method for that. 223 00:10:25,060 --> 00:10:29,580 Remember, module methods start with self, and we'll just call it random. 224 00:10:29,580 --> 00:10:34,300 And then inside of there, Ruby has a handy little method on arrays. 225 00:10:34,300 --> 00:10:39,420 Remember that snacks is an array, and you can call sample on an array, and it will return 226 00:10:39,420 --> 00:10:41,860 a random element from that array. 227 00:10:41,860 --> 00:10:45,700 So then if we wanted to get a random snack, we just assign it to a variable. 228 00:10:45,700 --> 00:10:48,780 If we wanted to, we'll say snack equals snack bar. 229 00:10:48,780 --> 00:10:53,600 And remember, we call module methods just with the module, so we'd say snackbar.random. 230 00:10:53,600 --> 00:11:03,020 And then we could print out something like enjoy your, remember snack has a name attribute. 231 00:11:03,020 --> 00:11:12,480 We could put in parentheses something like, oh, I don't know, snack.carbs.carbs. 232 00:11:12,480 --> 00:11:14,460 Enjoy your candy, 15 carbs. 233 00:11:14,460 --> 00:11:16,060 How's that? 234 00:11:16,060 --> 00:11:19,300 Now we have this little example code at the bottom of a module. 235 00:11:19,300 --> 00:11:24,020 We're gonna be requiring this module in other files, or requiring this file in other files. 236 00:11:24,020 --> 00:11:28,260 So remember the little trick, we might wanna surround this by this if statement so that 237 00:11:28,260 --> 00:11:32,300 this code only runs when we run this file. 238 00:11:32,300 --> 00:11:33,860 So we've got that all set up. 239 00:11:33,860 --> 00:11:35,480 Okay, this is great. 240 00:11:35,480 --> 00:11:40,700 Now our snack bar is full of snacks, so let's print off a list of all of our snacks, kind 241 00:11:40,700 --> 00:11:42,180 of like a menu of our options. 242 00:11:42,180 --> 00:11:46,180 Yeah, and then every time a movie's viewed, then we can just print out a random snack. 243 00:11:46,180 --> 00:11:47,180 Right. 244 00:11:47,180 --> 00:11:49,380 So let's go over to our playlist. 245 00:11:49,380 --> 00:11:53,540 And at the very top, we wanna make sure I'm requiring our new snack bar module. 246 00:11:53,540 --> 00:11:55,180 We're gonna be using that. 247 00:11:55,180 --> 00:11:57,939 So the first thing we wanna do is down in our play method. 248 00:11:57,939 --> 00:12:01,880 Before we do any viewings, we're gonna print out our menu of snacks here. 249 00:12:01,880 --> 00:12:03,640 So I'm gonna create a variable. 250 00:12:03,640 --> 00:12:05,660 It's gonna be snackbar. 251 00:12:05,660 --> 00:12:09,060 We're just gonna get that array snacks. 252 00:12:09,060 --> 00:12:20,900 And then we'll just print out something like, there are snacks.size snacks available in 253 00:12:20,900 --> 00:12:24,699 the snack bar, just like that. 254 00:12:24,699 --> 00:12:28,300 And then what we wanna do is we wanna iterate through all of those snacks and print them 255 00:12:28,300 --> 00:12:29,300 out. 256 00:12:29,300 --> 00:12:30,300 So we've got an array. 257 00:12:30,300 --> 00:12:31,300 We know how to iterate through those. 258 00:12:31,300 --> 00:12:32,300 We use the each method to do that. 259 00:12:32,300 --> 00:12:33,979 It takes a block. 260 00:12:33,979 --> 00:12:36,780 That block is going to pass us snack objects. 261 00:12:36,780 --> 00:12:39,220 Remember, we created those with our struct. 262 00:12:39,220 --> 00:12:41,020 So we've got a full snack object there. 263 00:12:41,020 --> 00:12:50,540 And we can just print out for each of these something like snack.name has snack.carbs 264 00:12:50,540 --> 00:12:51,540 carbs. 265 00:12:51,540 --> 00:12:57,220 Oh, and I just noticed this should be our snacks array, plural right there. 266 00:12:57,220 --> 00:12:58,920 That's the name of our array. 267 00:12:58,920 --> 00:13:00,300 So let's just go ahead and try that. 268 00:13:00,300 --> 00:13:01,300 We go over to Flix. 269 00:13:01,300 --> 00:13:05,740 If we run it from the top to bottom, we got our playlist. 270 00:13:05,740 --> 00:13:08,780 And then it says there are five snacks available in the snack bar. 271 00:13:08,780 --> 00:13:09,780 This is awesome. 272 00:13:09,780 --> 00:13:12,420 All right, so let's do the second part of that. 273 00:13:12,420 --> 00:13:13,660 Go back over to the playlist. 274 00:13:13,660 --> 00:13:17,380 Every time a movie is viewed, we wanna pick a random snack. 275 00:13:17,380 --> 00:13:22,620 So we'll do that after Waldorf and Stadler have given us a thumbs up or a thumbs down. 276 00:13:22,620 --> 00:13:24,940 So we'll just get a snack we call the snack bar. 277 00:13:24,940 --> 00:13:27,920 We call that random method that we wrote. 278 00:13:27,920 --> 00:13:32,220 And then we'll just print out something like movie.title. 279 00:13:32,220 --> 00:13:33,300 Movie's a local variable here. 280 00:13:33,300 --> 00:13:35,380 We're getting this in this block parameter. 281 00:13:35,380 --> 00:13:40,060 Movie.title led to snack.carbs. 282 00:13:40,060 --> 00:13:42,900 We're gonna do this reverse. 283 00:13:42,900 --> 00:13:44,660 We'll see how this works out. 284 00:13:44,660 --> 00:13:51,380 Snack.name carbs, oops, carbs being consumed. 285 00:13:51,380 --> 00:13:54,260 All right, so we're gonna switch the order here. 286 00:13:54,260 --> 00:13:55,860 Go back, have a look at that. 287 00:13:55,860 --> 00:13:57,540 Okay, we're gonna get a random name. 288 00:13:57,540 --> 00:13:59,660 We're gonna print it out, and then we're gonna print out the full movie. 289 00:13:59,660 --> 00:14:02,200 So let's go run that. 290 00:14:02,200 --> 00:14:04,140 We've got our listing at the top. 291 00:14:04,140 --> 00:14:09,060 Now as we're viewing things, Goonies got skipped, but it says Goonies led to 10 pretzel carbs 292 00:14:09,060 --> 00:14:10,500 being consumed. 293 00:14:10,500 --> 00:14:15,660 Goldfinger, well, he tallied up some nachos, as did Ghostbusters. 294 00:14:15,660 --> 00:14:17,060 And then we see our pretzel down here. 295 00:14:17,060 --> 00:14:21,780 So we've got all of our candies being randomly selected, or all of our snacks being randomly 296 00:14:21,780 --> 00:14:23,300 selected when the movie's viewed. 297 00:14:23,300 --> 00:14:27,580 Okay, now we teased you at the beginning of this section by talking about a treasure hunt. 298 00:14:27,580 --> 00:14:30,700 Well, now you get to introduce that concept in your game. 299 00:14:30,700 --> 00:14:34,620 Each time a player takes a turn, they'll find a random treasure. 300 00:14:34,620 --> 00:14:37,740 For now, you'll just print out the random treasure that was found. 301 00:14:37,740 --> 00:14:42,300 And in the next section, we'll talk about using hashes to accumulate the treasures for 302 00:14:42,300 --> 00:14:43,340 each player. 303 00:14:43,340 --> 00:15:02,340 So come on back when you're done. 25419

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