All language subtitles for pragstudio-ruby-09-arrays (Transcribed on 27-Apr-2023 20-40-52)

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,920 So now that you've created a bunch of similar objects, it's common to want to put them in 2 00:00:06,920 --> 00:00:08,120 a collection. 3 00:00:08,120 --> 00:00:12,040 And Ruby has two built-in collection classes, arrays and hashes. 4 00:00:12,040 --> 00:00:13,960 We're going to start by looking at arrays. 5 00:00:13,960 --> 00:00:17,800 We'll put our movie objects inside of an array, and then in the exercise, you can create an 6 00:00:17,800 --> 00:00:20,040 array for your player objects. 7 00:00:20,040 --> 00:00:23,280 Now an array is an ordered list of object references. 8 00:00:23,280 --> 00:00:26,440 Let's say we have three objects, the names of Muppets. 9 00:00:26,440 --> 00:00:30,640 You want to put them in an array to represent their seating arrangement at a movie. 10 00:00:30,640 --> 00:00:34,920 So to create an array, we would surround the objects with square brackets and separate 11 00:00:34,920 --> 00:00:36,520 them with commas. 12 00:00:36,520 --> 00:00:40,100 We would then assign the array to a variable seats. 13 00:00:40,100 --> 00:00:43,680 Each slot or position in the array holds a reference to an object. 14 00:00:43,680 --> 00:00:46,520 In this case, each object is a string. 15 00:00:46,520 --> 00:00:51,280 Now each position is identified by an integer, and with arrays, you always start counting 16 00:00:51,280 --> 00:00:52,500 with zero. 17 00:00:52,500 --> 00:00:57,360 How to access a particular object in the array, you index into its position. 18 00:00:57,360 --> 00:01:00,360 To show you that, let's look at this array in IRB, Mike. 19 00:01:00,360 --> 00:01:05,760 Sure, we'll set up the seats array just like you had them, and we create an array literal 20 00:01:05,760 --> 00:01:08,000 by using the square bracket syntax. 21 00:01:08,000 --> 00:01:13,500 So our first element will make it, let's see, Kermit, and the second element will be Fuzzy, 22 00:01:13,500 --> 00:01:17,980 and then finally we'll have Gonzo, and then we end it with the square bracket. 23 00:01:17,980 --> 00:01:19,160 So there we've got an array. 24 00:01:19,160 --> 00:01:20,760 We can print out our variable. 25 00:01:20,760 --> 00:01:25,120 Sure enough, we've got the three Muppets all seated together at the movies. 26 00:01:25,120 --> 00:01:28,080 As Nicole said, we can index into an array using integers. 27 00:01:28,080 --> 00:01:31,600 So the first element in the array is always at position zero. 28 00:01:31,600 --> 00:01:32,600 There's Kermit. 29 00:01:32,600 --> 00:01:37,440 Fuzzy's at position one, and then Gonzo is at position two. 30 00:01:37,440 --> 00:01:40,980 Now if we look at position three, there's nobody seated there. 31 00:01:40,980 --> 00:01:42,520 We get back nil. 32 00:01:42,520 --> 00:01:44,360 Nil's actually an object in Ruby. 33 00:01:44,360 --> 00:01:49,360 It represents the absence of an object, so there's nothing there, and so Ruby just returns 34 00:01:49,360 --> 00:01:50,360 nil to us. 35 00:01:50,360 --> 00:01:55,000 I want to also show you this handy shortcut for creating arrays of words. 36 00:01:55,000 --> 00:02:01,039 Instead of having to do the quotes that way, we can use this %w syntax, and then we use 37 00:02:01,039 --> 00:02:04,280 parentheses, and inside the parentheses we just type the words. 38 00:02:04,280 --> 00:02:06,080 We don't have to create strings at all. 39 00:02:06,080 --> 00:02:12,120 So it'd be Kermit, no comma necessary there, and then Gonzo, and we've got an array of 40 00:02:12,120 --> 00:02:13,120 words. 41 00:02:13,120 --> 00:02:16,440 So that's just a really easy way to do it if you've just got single words you want to 42 00:02:16,440 --> 00:02:17,440 create an array out of. 43 00:02:17,440 --> 00:02:20,800 Okay, in this case we started with an array that already had some elements, but let's 44 00:02:20,800 --> 00:02:22,600 start over and we'll build our array up. 45 00:02:22,600 --> 00:02:23,760 We'll start with an empty array. 46 00:02:23,760 --> 00:02:27,600 We'll just use the literal syntax, the square brackets there. 47 00:02:27,600 --> 00:02:31,400 The other way we could do this, just to show you that a class is being used, this is the 48 00:02:31,400 --> 00:02:35,960 same as calling the array class new method, and that'll give us back an empty array as 49 00:02:35,960 --> 00:02:36,960 well. 50 00:02:36,960 --> 00:02:38,240 So let's fill up these seats. 51 00:02:38,240 --> 00:02:42,600 Let's say in slot zero we can just assign to that element of the array. 52 00:02:42,600 --> 00:02:46,120 We're going to put Kermit, and I'm going to do these out of order because it doesn't matter 53 00:02:46,120 --> 00:02:47,400 how we fill these up. 54 00:02:47,400 --> 00:02:49,680 I'm going to put in seat number two. 55 00:02:49,680 --> 00:02:58,920 This is going to be Gonzo, and then in seat one, this will be Fozzie, like that. 56 00:02:58,920 --> 00:03:00,860 We print it out. 57 00:03:00,860 --> 00:03:02,440 We get Kermit, Fozzie, and Gonzo. 58 00:03:02,440 --> 00:03:07,400 So we just assigned strings to each slot or position inside of the array. 59 00:03:07,400 --> 00:03:09,080 Let's put Miss Piggy in the mix. 60 00:03:09,080 --> 00:03:10,520 Ah, Miss Piggy, yes. 61 00:03:10,520 --> 00:03:11,520 Okay, but wait. 62 00:03:11,520 --> 00:03:14,360 She doesn't want to sit next to Gonzo, so put her in four. 63 00:03:14,360 --> 00:03:15,960 Okay, so she can't go in three. 64 00:03:15,960 --> 00:03:17,760 We'll put her right there at four. 65 00:03:17,760 --> 00:03:19,760 This would be Miss Piggy. 66 00:03:19,760 --> 00:03:22,920 All right, and now let's look at our array. 67 00:03:22,920 --> 00:03:27,520 Well we've got actually three elements here, but the fourth position has nil in there. 68 00:03:27,520 --> 00:03:33,480 So gaps in arrays in Ruby are always filled with nil, which is the absence of an object. 69 00:03:33,480 --> 00:03:37,000 Arrays in Ruby are objects, which means we can also call methods on arrays. 70 00:03:37,000 --> 00:03:41,800 So let's just start with an empty array again, seats, and we'll fill it up using some method 71 00:03:41,800 --> 00:03:42,800 calls. 72 00:03:42,800 --> 00:03:43,800 So I'm going to take seats. 73 00:03:43,800 --> 00:03:48,720 I'm going to push on Kermit, he's the first Muppet to go in there, and then I can also 74 00:03:48,720 --> 00:03:52,760 push on Fozzie. 75 00:03:52,760 --> 00:03:56,180 So now if I look at seats, I've got two elements inside of there. 76 00:03:56,180 --> 00:03:58,400 Another way to do this is to use the append operator. 77 00:03:58,400 --> 00:04:03,920 So if we want a Gonzo, we just append, it'll append it to the end of the array. 78 00:04:03,920 --> 00:04:07,200 Another method we can call is we can call size. 79 00:04:07,200 --> 00:04:09,480 We've got three elements inside of the array. 80 00:04:09,480 --> 00:04:13,720 Now let's say we kind of want to treat this like a stack, where the last Muppet that went 81 00:04:13,720 --> 00:04:16,839 into the seats is the first one to come out when the movie's over. 82 00:04:16,839 --> 00:04:19,320 So we could call the pop method. 83 00:04:19,320 --> 00:04:23,920 That's going to return the last element of the array, and it actually removes it from 84 00:04:23,920 --> 00:04:24,920 the array. 85 00:04:24,920 --> 00:04:28,000 So now our seats array has Kermit and Fozzie inside of there. 86 00:04:28,000 --> 00:04:33,640 We could call pop one more time, then Fozzie comes out, finally, last time, Kermit. 87 00:04:33,640 --> 00:04:35,760 We look at our seats array, it's empty. 88 00:04:35,760 --> 00:04:40,120 We can actually turn around and call the empty question mark method, and sure enough, it 89 00:04:40,120 --> 00:04:41,360 returns true. 90 00:04:41,360 --> 00:04:43,880 Now arrays can actually hold any object reference. 91 00:04:43,880 --> 00:04:46,440 With the Muppets, we put string objects in the array. 92 00:04:46,440 --> 00:04:50,880 Yeah, but returning back to our movies example, we want to put movie objects inside of that 93 00:04:50,880 --> 00:04:51,880 array. 94 00:04:51,880 --> 00:04:53,500 So let's take a look at that. 95 00:04:53,500 --> 00:04:58,080 So back over in our flix.rb file, I've collapsed this class definition just to give us a little 96 00:04:58,080 --> 00:04:59,320 bit more space here. 97 00:04:59,320 --> 00:05:02,320 And we have three movies, movie one, two, and three. 98 00:05:02,320 --> 00:05:04,440 And we just want to put those inside of an array. 99 00:05:04,440 --> 00:05:06,920 So I'm going to create a variable called movies. 100 00:05:06,920 --> 00:05:13,560 I'm going to create an array movie one, movie two, and movie three. 101 00:05:13,560 --> 00:05:14,560 We've got all those in array. 102 00:05:14,560 --> 00:05:17,640 Again, an array can hold any arbitrary object here. 103 00:05:17,640 --> 00:05:19,080 And then I just want to print out the movies. 104 00:05:19,080 --> 00:05:24,120 Now it turns out if we call put s and we pass it an array, what it will do is call the to 105 00:05:24,120 --> 00:05:27,960 s method on each of the objects inside of that array. 106 00:05:27,960 --> 00:05:30,860 And we've already defined a to s method in our movie class. 107 00:05:30,860 --> 00:05:35,780 So when we run this, we get our movies printed out just like the listing we expect. 108 00:05:35,780 --> 00:05:39,080 Now that works for printing the movies, but let's suppose we also want to thumbs up or 109 00:05:39,080 --> 00:05:41,679 thumbs down each of the movies in our array. 110 00:05:41,679 --> 00:05:45,440 Well Ruby has some primitive looping constructs that you might be familiar with from other 111 00:05:45,440 --> 00:05:49,059 programming languages, things like while and for loops. 112 00:05:49,059 --> 00:05:54,919 But collection classes in Ruby, arrays for example, have these built in iterator methods. 113 00:05:54,919 --> 00:05:57,599 One of those built in iterator methods is the each method. 114 00:05:57,599 --> 00:06:03,520 We just take our array and we call each and then we pass each a block. 115 00:06:03,520 --> 00:06:07,719 And what each is going to do is for every element in the array, it's going to call our 116 00:06:07,719 --> 00:06:11,560 block and it's going to pass in each movie to fill in this variable here. 117 00:06:11,560 --> 00:06:14,200 I'm going to call the variable movie. 118 00:06:14,200 --> 00:06:16,599 Then inside of the block, we can use that variable. 119 00:06:16,599 --> 00:06:19,080 It's like a local variable and we can do something with it. 120 00:06:19,080 --> 00:06:23,440 So let's say we want to thumbs up this movie and then we'll just call put s on the movie 121 00:06:23,440 --> 00:06:26,479 so that we get the full listing again. 122 00:06:26,479 --> 00:06:28,760 If I run that, we get the same thing. 123 00:06:28,760 --> 00:06:30,280 Goonies has a rank of 11. 124 00:06:30,280 --> 00:06:35,000 Notice that it's gone up by one in rank because we thumbs up it before we actually printed 125 00:06:35,000 --> 00:06:36,000 it. 126 00:06:36,000 --> 00:06:38,840 So you can think of each as being like a little loop. 127 00:06:38,840 --> 00:06:45,000 It calls this block for every element in the array, assigning each element to this variable 128 00:06:45,000 --> 00:06:48,400 movie that we can then use inside of the block. 129 00:06:48,400 --> 00:06:52,080 Now to demonstrate that movie is actually just a variable, let's change it to m. 130 00:06:52,080 --> 00:06:53,080 Sure. 131 00:06:53,080 --> 00:06:54,320 Yeah, we can just change that to m. 132 00:06:54,320 --> 00:06:59,719 As long as inside of the block then, we change each of those to m, we can do that. 133 00:06:59,720 --> 00:07:03,380 Because this name doesn't have to match any of the variables outside of here. 134 00:07:03,380 --> 00:07:07,480 It's just a block parameter and we can call it whatever we want. 135 00:07:07,480 --> 00:07:08,480 So let's recap. 136 00:07:08,480 --> 00:07:12,760 We have an array of movie objects assigned to the movie's variable. 137 00:07:12,760 --> 00:07:15,840 All Ruby arrays have an each method that acts like a loop. 138 00:07:15,840 --> 00:07:18,880 It iterates through each element in the array. 139 00:07:18,880 --> 00:07:20,720 So what does it do with each movie? 140 00:07:20,720 --> 00:07:22,300 Well that's up to us. 141 00:07:22,300 --> 00:07:25,220 We associate a code block with the each method. 142 00:07:25,220 --> 00:07:28,240 It's just a chunk of code between do and end. 143 00:07:28,240 --> 00:07:33,200 Each calls the block for each element in the array and signs it to the movie variable. 144 00:07:33,200 --> 00:07:37,640 So in the first iteration through the loop, the first object in our array is assigned 145 00:07:37,640 --> 00:07:39,940 to the variable named movie. 146 00:07:39,940 --> 00:07:42,120 Then the block of code runs. 147 00:07:42,120 --> 00:07:45,980 Now movie is a local variable used only in this block. 148 00:07:45,980 --> 00:07:50,620 So here our movie rank is changed from 10 to 11 and our code prints out, Goonies has 149 00:07:50,620 --> 00:07:52,260 a rank of 11. 150 00:07:52,260 --> 00:07:56,120 The code then loops through all the other movie objects in the array, changing each 151 00:07:56,120 --> 00:07:59,040 movie's ranking and then printing it out. 152 00:07:59,040 --> 00:08:01,240 Now don't let the syntax throw you here. 153 00:08:01,240 --> 00:08:04,400 We're going to look at blocks in more detail later. 154 00:08:04,400 --> 00:08:06,380 Now arrays are kind of fun if you ask me. 155 00:08:06,380 --> 00:08:08,580 So here's your chance to practice with them. 156 00:08:08,580 --> 00:08:12,320 In the exercise, you're going to play with them a little bit in IRB and then you're going 157 00:08:12,320 --> 00:08:13,320 to put them in your game. 158 00:08:13,320 --> 00:08:18,540 Yeah, and in the last couple of sections we've looked at creating classes and using arrays. 159 00:08:18,540 --> 00:08:27,680 So in the next section we're going to put all this together. 14369

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