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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So now that you've created a bunch of similar objects, it's common to want to put them in

a collection.

And Ruby has two built-in collection classes, arrays and hashes.

We're going to start by looking at arrays.

We'll put our movie objects inside of an array, and then in the exercise, you can create an

array for your player objects.

Now an array is an ordered list of object references.

Let's say we have three objects, the names of Muppets.

You want to put them in an array to represent their seating arrangement at a movie.

So to create an array, we would surround the objects with square brackets and separate

them with commas.

We would then assign the array to a variable seats.

Each slot or position in the array holds a reference to an object.

In this case, each object is a string.

Now each position is identified by an integer, and with arrays, you always start counting

with zero.

How to access a particular object in the array, you index into its position.

To show you that, let's look at this array in IRB, Mike.

Sure, we'll set up the seats array just like you had them, and we create an array literal

by using the square bracket syntax.

So our first element will make it, let's see, Kermit, and the second element will be Fuzzy,

and then finally we'll have Gonzo, and then we end it with the square bracket.

So there we've got an array.

We can print out our variable.

Sure enough, we've got the three Muppets all seated together at the movies.

As Nicole said, we can index into an array using integers.

So the first element in the array is always at position zero.

There's Kermit.

Fuzzy's at position one, and then Gonzo is at position two.

Now if we look at position three, there's nobody seated there.

We get back nil.

Nil's actually an object in Ruby.

It represents the absence of an object, so there's nothing there, and so Ruby just returns

nil to us.

I want to also show you this handy shortcut for creating arrays of words.

Instead of having to do the quotes that way, we can use this %w syntax, and then we use

parentheses, and inside the parentheses we just type the words.

We don't have to create strings at all.

So it'd be Kermit, no comma necessary there, and then Gonzo, and we've got an array of

words.

So that's just a really easy way to do it if you've just got single words you want to

create an array out of.

Okay, in this case we started with an array that already had some elements, but let's

start over and we'll build our array up.

We'll start with an empty array.

We'll just use the literal syntax, the square brackets there.

The other way we could do this, just to show you that a class is being used, this is the

same as calling the array class new method, and that'll give us back an empty array as

well.

So let's fill up these seats.

Let's say in slot zero we can just assign to that element of the array.

We're going to put Kermit, and I'm going to do these out of order because it doesn't matter

how we fill these up.

I'm going to put in seat number two.

This is going to be Gonzo, and then in seat one, this will be Fozzie, like that.

We print it out.

We get Kermit, Fozzie, and Gonzo.

So we just assigned strings to each slot or position inside of the array.

Let's put Miss Piggy in the mix.

Ah, Miss Piggy, yes.

Okay, but wait.

She doesn't want to sit next to Gonzo, so put her in four.

Okay, so she can't go in three.

We'll put her right there at four.

This would be Miss Piggy.

All right, and now let's look at our array.

Well we've got actually three elements here, but the fourth position has nil in there.

So gaps in arrays in Ruby are always filled with nil, which is the absence of an object.

Arrays in Ruby are objects, which means we can also call methods on arrays.

So let's just start with an empty array again, seats, and we'll fill it up using some method

calls.

So I'm going to take seats.

I'm going to push on Kermit, he's the first Muppet to go in there, and then I can also

push on Fozzie.

So now if I look at seats, I've got two elements inside of there.

Another way to do this is to use the append operator.

So if we want a Gonzo, we just append, it'll append it to the end of the array.

Another method we can call is we can call size.

We've got three elements inside of the array.

Now let's say we kind of want to treat this like a stack, where the last Muppet that went

into the seats is the first one to come out when the movie's over.

So we could call the pop method.

That's going to return the last element of the array, and it actually removes it from

the array.

So now our seats array has Kermit and Fozzie inside of there.

We could call pop one more time, then Fozzie comes out, finally, last time, Kermit.

We look at our seats array, it's empty.

We can actually turn around and call the empty question mark method, and sure enough, it

returns true.

Now arrays can actually hold any object reference.

With the Muppets, we put string objects in the array.

Yeah, but returning back to our movies example, we want to put movie objects inside of that

array.

So let's take a look at that.

So back over in our flix.rb file, I've collapsed this class definition just to give us a little

bit more space here.

And we have three movies, movie one, two, and three.

And we just want to put those inside of an array.

So I'm going to create a variable called movies.

I'm going to create an array movie one, movie two, and movie three.

We've got all those in array.

Again, an array can hold any arbitrary object here.

And then I just want to print out the movies.

Now it turns out if we call put s and we pass it an array, what it will do is call the to

s method on each of the objects inside of that array.

And we've already defined a to s method in our movie class.

So when we run this, we get our movies printed out just like the listing we expect.

Now that works for printing the movies, but let's suppose we also want to thumbs up or

thumbs down each of the movies in our array.

Well Ruby has some primitive looping constructs that you might be familiar with from other

programming languages, things like while and for loops.

But collection classes in Ruby, arrays for example, have these built in iterator methods.

One of those built in iterator methods is the each method.

We just take our array and we call each and then we pass each a block.

And what each is going to do is for every element in the array, it's going to call our

block and it's going to pass in each movie to fill in this variable here.

I'm going to call the variable movie.

Then inside of the block, we can use that variable.

It's like a local variable and we can do something with it.

So let's say we want to thumbs up this movie and then we'll just call put s on the movie

so that we get the full listing again.

If I run that, we get the same thing.

Goonies has a rank of 11.

Notice that it's gone up by one in rank because we thumbs up it before we actually printed

it.

So you can think of each as being like a little loop.

It calls this block for every element in the array, assigning each element to this variable

movie that we can then use inside of the block.

Now to demonstrate that movie is actually just a variable, let's change it to m.

Sure.

Yeah, we can just change that to m.

As long as inside of the block then, we change each of those to m, we can do that.

Because this name doesn't have to match any of the variables outside of here.

It's just a block parameter and we can call it whatever we want.

So let's recap.

We have an array of movie objects assigned to the movie's variable.

All Ruby arrays have an each method that acts like a loop.

It iterates through each element in the array.

So what does it do with each movie?

Well that's up to us.

We associate a code block with the each method.

It's just a chunk of code between do and end.

Each calls the block for each element in the array and signs it to the movie variable.

So in the first iteration through the loop, the first object in our array is assigned

to the variable named movie.

Then the block of code runs.

Now movie is a local variable used only in this block.

So here our movie rank is changed from 10 to 11 and our code prints out, Goonies has

a rank of 11.

The code then loops through all the other movie objects in the array, changing each

movie's ranking and then printing it out.

Now don't let the syntax throw you here.

We're going to look at blocks in more detail later.

Now arrays are kind of fun if you ask me.

So here's your chance to practice with them.

In the exercise, you're going to play with them a little bit in IRB and then you're going

to put them in your game.

Yeah, and in the last couple of sections we've looked at creating classes and using arrays.

So in the next section we're going to put all this together.

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