Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now as the game stands now, you're sort of teasing the player, right?
They can find a treasure, but they don't actually get to keep it.
We aren't storing the treasure for each player.
In the same way when we play a movie, we're picking a random snack, but we're not actually
storing that snack with the movie.
So in this section, we want to use a hash to store the snack name and then accumulate
the total carbs for that snack.
Now hash is an index collection of object references.
You'll sometimes hear it called a dictionary, map, or an associative array.
It's useful when you want to keep track of two things.
You can think of it as a two-column table with keys and values.
For example, let's say we want to keep track of snack names and their associated carbs.
The key is the name of the snack and the value is the number of carbs.
The key and the value make a pair.
So we could create the hash like this, snack carbs equals our keys become symbols that
are associated with values and we surround it with curly braces.
Then to get a value, we would index the hash with the values associated key.
Mike, can we look at this hash in IRB?
Sure.
All right, let's just put our hash in a variable called snack carbs, like that, and then we'll
start our hash here.
And then we've got three different types of snacks.
So we've got candy, we'll use a symbol as the key because they work really well as keys.
It's got carbs of 15.
Pretzel has 10 and we've got soda and it has five.
So there we have our hash.
Now to get a particular value, as Nicole said, we just take our hash and then we pass in
the key or we index to the key.
So candy has 15.
Pretzel has 10, just like that.
In Ruby 1.9, if you use symbols as your keys, there's a slightly different syntax that you
can use.
So in Ruby 1.9, we can create our hash like this, snack carbs, same hash literal there,
but because we're using symbols, if we want to use symbols, we can just do this.
Candy is 30, pretzel is 100, and soda is 50.
So you've got the key, candy, like that, and then you just put a colon and you put the
value.
So you can use either of these syntaxes.
You can use the equal and greater than sign or you can do it just like this if you're
using Ruby 1.9.
So we started with a hash that already had keys and values inside of that.
Let's turn it around.
We'll start with an empty hash.
We can either do that by creating an empty hash that way or we could use snack carbs
equals hash.new.
Exact same thing.
It's going to create a new hash using the hash class in Ruby.
And let's just build it up this way.
Let's say we've got snack carbs and we want to assign the key candy the value of 15, like
that.
Now when we index snack carbs candy, we get back 15.
So let's put a couple more in there.
Let's say we want to put in pretzel like that and we're going to assign it 0, 10.
I think I did 100 last time, so we'll give pretzel 100.
Index that.
Sure enough, we have 100.
How about soda?
Well, we'll put in, fill up the hash that way and we get back five.
So now if we look at the hash, snack carbs, there's our hash statement there.
And hashes are objects in Ruby, so lots of methods on hashes.
Some of the more convenient ones would be something like if we wanted to get all the
keys, it'll return an array of the keys, candy, pretzel, and soda.
Or maybe we wanted to just get all the values.
It returns an array of values.
Now like arrays, you can iterate through a hash.
Let's take a look at how that works.
Here's our hash of snacks and carbs.
Like arrays, hashes have an each method and that takes a block.
If we declare the block to take two parameters, the first will be the key and the second will
be its value.
So when we iterate through this hash, the first time through candy is assigned to the
name variable and 15 is assigned to the carbs variable and we get candy has 15 carbs.
Then we iterate through the rest of the hash and we get pretzel has 10 carbs and soda has
five carbs.
So let's show how this works in IRB, Mike.
Sure, we'll just use our snack carbs hash.
We'll call the each method that takes a block, single line block.
I'll just use braces there and it passes as two block parameters, the key and the value.
And then inside there, I'll just do a put s and I'll say key has value carbs.
Candy has 15 carbs, pretzel has 100, and soda has five.
So we've seen how to set keys and values in a hash, but we actually want to use this hash
to accumulate the total carbs for any particular snack.
So let's look at how to do that.
So let's just start over here with our snack carbs and we're just going to use a empty
hash for now.
And let's say that when a movie runs, it caused a candy to be consumed.
So we want to assign the value 15 to candy.
So candy, we've got 15 carbs.
Now let's say we run the next viewing and another candy snack got consumed.
So we want to add 15 to the existing 15.
Well, we can do that using plus equals.
It's going to take the value that's in the candy or that's associated with the candy
key, add 15 to it.
So if we look at our snack carbs hash now, it's got candy 30.
It updated the value for that key.
So what happens if we index into the hash with a key that doesn't have a value?
Let's say soda in this case.
Well, we get back nil.
There's no value associated with that.
And if we were to try to accumulate soda, so we haven't consumed a soda, but if we were
to use the syntax plus equals to add five to that existing key, well, we have ourselves
a problem because Ruby's trying to add nil, which is the value that's currently associated
with that key, and five together.
And that's just not going to work.
So we kind of need a way to set up our hash to instead of having a default value of nil
for keys that don't have a value, instead have it have a default value of zero.
And the way we do that is when we create the hash, this is snack carbs, we can create it
using the hash class, pass in new or call the new method, and then pass in what the
default value should be.
So we're going to say the default value should be zero.
That way, if we call snack carbs and we ask for soda, instead of getting back nil, we
get back zero.
And this is good because then we can go ahead and add things.
They may not already exist there, and we won't get this error.
So if we were to say add plus equals five, well, we just get five.
And if we were to consume another soda, well, we're going to end up with 10.
We're going to need to do something like that inside of our movie application so that snacks
always have a carb total of zero starting out.
So here's what we have now.
We have our snack bar, and when we play a movie, we call the random method, and we get
a snack object, and we print it out.
But we aren't storing it with the movie.
Now what we want to do is store the snacks that were consumed during each movie.
Inside the movie class, we won't store the snack object itself.
Instead we'll store the snack name and its accumulated carbs in a hash.
In the hash, the key will become the name of the snack, and the value will be the accumulated
carbs.
That way our hash fills up like this, popcorn for 20, candy for 15, nachos for 40, but then
when we add the second candy that comes back around, candy will go to 30.
Bear in mind, however, that we want to accumulate snacks for each movie, so we need a hash for
each movie.
We'll use a snack carbs instance variable.
That way, as we iterate through the movies, each movie will have a hash in which to accumulate
its snacks.
We'll need an instance variable for that.
Remember that values of instance variables are unique for each object.
So let's go do that.
Returning to our playlist class here, every time we play a movie, we're grabbing this
random snack, and now we want to hand that off to the movie somehow.
So I'm just going to take this line of code right here.
We've got a movie object.
What we want to be able to do is call something like movie8snack, and then pass in that snack.
That way the movie can take credit for the snack that was consumed while it was being
viewed.
We're going to take this out of here too, we don't need that in there anymore.
So then over in my movie class, I'm going to need to store these snacks in a hash, as
Nicole said.
So when I set up my movie class, I'm going to go ahead and initialize a snack carbs hash.
And remember, I want to use this syntax.
I want it to default to values of zero.
So if we ask it for a snack that's not there, it's just going to say it has zero carbs.
Now we need to define this 8snack method that we are calling over in Playlist.
So I'm just going to do it right up here.
It's going to be called 8snack, and we're going to pass in a snack object here.
So what do we want to do inside of here?
Well, we want to accumulate things inside of our snack carbs hash, snack carbs.
So the key is going to be the name of the snack.
Remember, the name of the snack is an attribute on the snack object.
It also happens to be a symbol, which works out nicely.
The value of that key is going to be the carbs of the snack.
Now if we did the assignment this way, then it's just always going to be the most recent
snack that got consumed.
We want to accumulate, so we want to do plus equals.
That way, as we accumulate more snacks, then that carb total will continue to go up.
And then at the end of this, I'm just going to print out the statement that we had before.
I can just print out our title led to snack carbs being consumed, just like we had before
there.
And then I'm going to put one more thing at the bottom, just so that we can see it.
I'm going to print out the movie title snacks.
And then I just want to print out the whole hash here, and I'm going to do that by doing
snack carbs.
And it'll just print out the hash for us.
So there's our method for eating the snack.
We can go back over to our Flix file now and go ahead and run it.
So let's just look at viewing one.
It says Goonies led to 15 snack carbs being consumed.
And then we're printing out Goonies snacks here, and we see that it just got one thing
inside of its array.
Now down here in the second viewing, make this a little bit bigger, the second viewing,
Goonies now has two snacks, candy 15, pretzel 10.
So here we can see in viewing three, Goonies has one candy, but notice that it's got a
pretzel total carb count of 20.
Pretzels only worth 10 each, so it's consumed two pretzels, or it led to two pretzels and
one candy being consumed.
So our accumulation in the hash is working as we expect.
Now what if we wanted to total up all the carbs consumed by the movie, like two pretzels
plus three nachos plus four sodas?
How many carbs is that?
Well, the total carbs that the movie consumed after all the viewings.
Right.
Well, you know, we can make pretty quick work of that using that reduce method we saw earlier.
So let's revisit that again.
Let's do that.
So let's say we have an array of carbs like this, 10, 20, 30, right?
And we want to sum all those up.
Well, we could use reduce.
One form of reduce takes a block.
The first block parameter is the accumulator, and then the element being passed in, and
we could say sum plus in, and we get back 60.
Another way to do that that we saw is we can pass a symbol and then an operation, let's
say the plus operation, and it will tally up all the carbs that way.
But what if the array is empty?
Well, another form of this is to pass in the initial value.
So if the carbs array is empty, we'll just get back zero.
Otherwise it'll tally all those up.
So we probably want to use this form just in case the movie hasn't consumed any carbs
yet.
So let's go back over to our movie class, and we can go ahead and define a method that
will tally up all the carbs.
So let's just call it carbs consumed.
And inside of that method, we're just going to take our snack carbs hash.
Remember we can get all the values of a hash by calling the values method.
This is going to be all the carb numbers, right?
And then we can call reduce on it because it's just an array.
We'll have an initial value of zero, and then we'll have it add all of those up.
So that's the method to total up all the carbs.
If we go back over to the playlist now to use that method, we'll do it down in print
stats.
Let's say we do it, oh, maybe at the top here before we print out the hits and the flops.
We'll just take our movies array.
We'll sort them because we can sort them.
We've got a sorting method now.
That's going to pass us a movie object.
And then we'll print out something like, let's see, movie.title, so that's a form snack totals
like that.
And then we'll print out, well, let's see, movie.
Now we can call our method carbs consumed, grand total carbs.
Go to our Flix file.
If we run it, we look down at the bottom here.
Here's Kermit stats.
So Ghostbusters, 45 grand total carbs, Goonies 30, and Goldfinger 40.
Now later we'll break this out on a per snack basis, but for now, let's tally up all the
carbs like all of Goonies carbs plus Ghostbusters plus Goldfinger's carbs.
So we're going to need a new method on the playlist to do that.
It's all the carbs for all the movies.
You're really liking this reduced method thing, aren't you?
Oh yeah, very much.
Let's try that.
So go back over to the playlist here, and what we want to do is maybe up here we want
to print something like, let's just set up a little goal for ourselves here.
We want to be able to call a method like total carbs consumed.
This is for the entire playlist, and then we'll print total carbs consumed just like
that.
So how do we implement that?
Well, we'll define our method total carbs consumed like that.
Give ourselves a little space.
So what we want to do is we want to loop through all the movies, and we want to accumulate
all of their carb totals.
So we're going to take our movies.
We can say reduce like that.
Now I'm going to pass a parameter to reduce zero.
This is going to be the initial value that it's going to use in case we don't have anything
in that movies array.
And we're going to give it a block.
Remember the block to reduce takes an accumulator sum, and then it's going to give us each movie.
So it's going to iterate through, but it's also going to reduce everything down to a
number.
So inside of there, we're just going to add to the accumulator sum.
We're going to add movie.
And remember, we just wrote this method on movie called carbs consumed.
So reduce the movies array down to the sum of all the carbs consumed across all the movies.
Right.
Right.
Go back to Flix.
We run that down in our stats, Kermit stats, 200 total carbs consumed, which should be
the total of 75, 65, and 60.
All right.
You guessed it.
Now it's your turn.
In the same way that we stored snacks for each movie, in the exercise, you're going
to store found treasures for each player.
And then you're going to add up the total treasure points and actually change your player's
score.
And coming up, we're going to write our own iterator method so that we can print a player's
points on a per treasure basis.
Have fun.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.