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 the game is coming along quite nicely.
You have multiple players, you have multiple numbers of rounds, random wooting and blamming
with a die, and you have some basic stats.
But it's time to introduce a new dimension to the game, a treasure hunt.
The idea is that the game has treasures, and each treasure has a name and a point value,
and we put them in a treasure trove.
Then when a player takes a turn, he finds a random treasure.
But of course we'll leave that for you in the exercise.
We're going to do something similar with the movie app.
Instead of treasures, we'll have snacks with a name and a card value.
We'll put them in a snack bar.
Then when a movie is played, a random snack gets consumed.
This gives us an opportunity to revisit symbols and also learn about structs.
We'll model both the treasure and the snack as a struct, and we'll see some symbols along
the way.
Now, we've actually seen symbols before.
Let's revisit them.
Now, symbols start with a colon.
Here health is a symbol.
When you see a symbol, think name, because that's all a symbol really is.
It's a convenient way to name or identify something in our code.
For example, here we're naming an attribute.
We want an attribute named health, or in this case, an attribute named name.
Or perhaps we have a method called find with an option named all.
Or we might want to refer to colors by name, a color named red.
Or let's say we had some sort of support ticket system where we wanted to represent the statuses
as open, closed, or pending.
We could use an array of symbols for that.
So symbols are just a way for us to name things in our code.
So let's start by experimenting with symbols in IRB.
So as Nicole said, symbols start with a colon.
So here's a symbol for the color green.
And if we look at the class of that thing, well, it's a symbol.
Now this is different from a single or double quoted string green.
We look at its class, well, it's a string.
So let's look at the object ID for our symbol, green.
All right, we get that number.
If we just use the same symbol again, they're exactly the same object ID.
Ruby makes sure there's only one value for the green symbol.
Green is green is green.
In other words, a particular symbol refers to the same symbol object.
In contrast to that, if we look at our string green, we look at object ID, we get that number.
If we create another string object here, well, we're going to get a different string object.
So strings are separate objects.
The green symbol is always the same object.
And these are two very different types in Ruby.
If we look at green, the string, see if it's equal to green, the symbol.
No it's not.
Again, they're different objects.
But it's fairly easy to convert from one to another.
So if we had the green symbol, we could convert it to a string simply by calling the to underscore
method, just like that.
Or if we had the green string, we could call the to symbol method and we get the symbol
for that.
So symbols are just used to identify or stand for something in our code.
You could think of it as a constant string if you want to.
It's actually a different type than that.
But we use them just to identify or stand for something.
In fact, if you look at the instance methods on the symbol class, you get something like
that.
Notice that we don't have all the methods that we had for a string.
If we look at strings instance methods, well it has a whole bunch more because you want
to do things like capitalize and reverse and get the length of.
You want to do some text processing with the string.
Now don't overthink symbols.
Here's the important distinction.
You use symbols when you want to name something like an attribute or an option.
And you use strings when you want string-like behavior or you want to do some text processing.
Yeah, and if you're still unsure about symbols, just go with the flow for now.
You've seen them for a while, using them becomes a lot more natural.
So let's look at a more practical example of symbols.
Now we want to model a snack, and it just has two attributes, name and carb count.
Yeah, we could write a class that we're used to, say something like snack, and then, oh
I don't know, we'll just use attribute reader and we could give it name and carbs because
you said those are the only attributes we really need, right?
We need to initialize it, initialize, we pass in name and we pass in carbs, and then we'd
have to assign those to instance variables, of course.
Carbs equals carbs, something like that.
Then we could create snacks from that class.
We've seen how to do that.
Let's say our snack is popcorn.
Popcorn.
Mmm, popcorn.
And the name of that would be popcorn, that's the name we pass in, and let's say it's got
a carb value of 20.
And then we could print out its name, because we have an attribute reader for that, and
we could print out its carbs, like that.
Let's just see if that works.
Sure enough, we've got popcorn with a carb value of 20.
So we could definitely do that, but I don't know what other behavior a snack's actually
going to have.
So it gives us an opportunity to talk about something different in Ruby, which is a struct.
And we can do the same thing that we have this class.
Instead of writing an explicit class, we can do this in a struct.
We'll just comment out our class, like that, and we'll assign to a constant called snack,
and then we say struct.new, and then we have to give it the names of the attributes we
want the struct to have.
Name and carbs, just like what we did with AtroReader up here.
It's got name and carbs.
This is struct.new.
So a struct is just a collection of attributes, and it saves us from having to write an explicit
class.
In fact, a struct generates a class object, just like a class definition did.
So we've got this constant snack, it starts with an uppercase letter.
It's as if we defined a class called snack, and we can use it just like we did before.
We can call snack.new, and we get the same thing.
We get popcorn and 20.
So we're using symbols here to denote the attributes that we want, because we're just
having an attribute named name or an attribute named carbs, and the struct will take care
of creating the accessor methods for that.
So we can get popcorn.name, for example, just as we would if we had attributes inside of
a class.
So should we create another snack?
Yeah, let's do that.
Let's create candy.
Candy.
All right, we use our constant snack, new.
The name of that is going to be just candy.
Okay, now we should take a moment here to point out that we are not the authority on
the nutritional value of snacks.
So let's make its carb value 15.
I don't really know if that's more or less than the carb value of popcorn.
Let's just say it's 15.
That sounds really good.
Right, right.
I really wish Snickers were 15.
Okay, so we've got some candy, so we can print out candy has a name and candy has carbs.
So now we've got two snack objects, popcorn and candy.
Now a snack may grow up to be a class someday, but for now a struct is all we need.
Yeah, we just have these attributes, so I'm just going to remove the class.
We could convert that to a class later if we wanted to, but let's just leave it a
struct for now.
Okay, this is cool.
Now we know how to create snacks.
Let's put them in a snack bar.
Hmm, a snack bar.
So how are we going to model that?
Well, we don't need multiple objects.
There's only going to be one snack bar in our entire program.
Right.
And we saw we did the same thing with reviewer earlier.
So let's make the snack bar a module.
So we're in our snackbar.rb file where we have this struct defined.
I'm just going to leave that at the top.
We want a new module.
The module is going to be called snackbar.
And we saw that we can put methods inside of modules or module methods inside of modules
earlier.
We can also put other things inside of modules.
For example, our snack bar, we kind of want an array of snacks that the snack bar has
inside of it.
So let's do that as a constant.
And a constant in Ruby is all uppercase characters, which can have a snacks constant.
And it's just going to be an array.
And what do we want inside of the snacks array?
Well, we want some snacks.
So we can use the struct that we defined earlier.
So I'm going to have a snack.
I'm going to call snack.new.
And we'll put, let's put popcorn in our snack bar.
Okay, popcorn.
Now, we could do it like this with a string popcorn.
What's the nutritional value of popcorn again?
20.
Okay.
So we really only can be referencing the names of these snacks inside of our program.
So let's just go ahead and do these as symbols because they're just something we're going
to name.
All right.
We're going to print them out to the screen, but that's going to be easy enough.
So we'll just leave them as symbols for now.
So we've got popcorn.
Give me a few other snacks here.
Oh, let's see.
We should put candy back in it.
Let's do 15.
Candy 15.
Let's do nachos.
Ooh, nachos.
All right.
And then, oh, let's put some pretzels in there.
Let's put some pretzels in.
Nachos was how many?
Oh, nachos.
Those are kind of expensive, 40.
Wow.
Well, look at all that cheese.
Let's do a pretzel for 10.
Pretzel for 10.
Okay.
And then we probably need something to drink.
So let's do, well, I call it pop.
People in other parts of the country call it soda.
Yeah, soda.
So it's low, like five.
Right.
Soda's cheap.
Okay.
So we've got our array of snacks now.
And it's inside of this module.
Right.
I left the snack struct outside of here because we might want to access that later outside
of the module, give or take whether you want to put it inside of the module.
But we're going to leave it outside for right now.
So just given that module, how would we print out all of our snacks?
Well, we'd use putS.
We use our module name, snackbar.
And then to get access to this constant inside of the module, it's sort of namespaced inside
of there.
To get access to it, we use the scope resolution operator, that's a double colon, and then
we just give it snacks, which is the constant.
So we're saying we want this constant inside of that module.
So we run that and we get a printout.
We've got all of our snacks.
You see the name is a symbol, naming it.
We've got the car values.
So we've got part of our snack bar set up here.
Now every time we play a movie, we want to select a random snack from the snack bar.
Oh, right.
Because we need some way to get one of these random things out of the array.
So we saw that we can define module methods earlier.
Let's just create a method for that.
Remember, module methods start with self, and we'll just call it random.
And then inside of there, Ruby has a handy little method on arrays.
Remember that snacks is an array, and you can call sample on an array, and it will return
a random element from that array.
So then if we wanted to get a random snack, we just assign it to a variable.
If we wanted to, we'll say snack equals snack bar.
And remember, we call module methods just with the module, so we'd say snackbar.random.
And then we could print out something like enjoy your, remember snack has a name attribute.
We could put in parentheses something like, oh, I don't know, snack.carbs.carbs.
Enjoy your candy, 15 carbs.
How's that?
Now we have this little example code at the bottom of a module.
We're gonna be requiring this module in other files, or requiring this file in other files.
So remember the little trick, we might wanna surround this by this if statement so that
this code only runs when we run this file.
So we've got that all set up.
Okay, this is great.
Now our snack bar is full of snacks, so let's print off a list of all of our snacks, kind
of like a menu of our options.
Yeah, and then every time a movie's viewed, then we can just print out a random snack.
Right.
So let's go over to our playlist.
And at the very top, we wanna make sure I'm requiring our new snack bar module.
We're gonna be using that.
So the first thing we wanna do is down in our play method.
Before we do any viewings, we're gonna print out our menu of snacks here.
So I'm gonna create a variable.
It's gonna be snackbar.
We're just gonna get that array snacks.
And then we'll just print out something like, there are snacks.size snacks available in
the snack bar, just like that.
And then what we wanna do is we wanna iterate through all of those snacks and print them
out.
So we've got an array.
We know how to iterate through those.
We use the each method to do that.
It takes a block.
That block is going to pass us snack objects.
Remember, we created those with our struct.
So we've got a full snack object there.
And we can just print out for each of these something like snack.name has snack.carbs
carbs.
Oh, and I just noticed this should be our snacks array, plural right there.
That's the name of our array.
So let's just go ahead and try that.
We go over to Flix.
If we run it from the top to bottom, we got our playlist.
And then it says there are five snacks available in the snack bar.
This is awesome.
All right, so let's do the second part of that.
Go back over to the playlist.
Every time a movie is viewed, we wanna pick a random snack.
So we'll do that after Waldorf and Stadler have given us a thumbs up or a thumbs down.
So we'll just get a snack we call the snack bar.
We call that random method that we wrote.
And then we'll just print out something like movie.title.
Movie's a local variable here.
We're getting this in this block parameter.
Movie.title led to snack.carbs.
We're gonna do this reverse.
We'll see how this works out.
Snack.name carbs, oops, carbs being consumed.
All right, so we're gonna switch the order here.
Go back, have a look at that.
Okay, we're gonna get a random name.
We're gonna print it out, and then we're gonna print out the full movie.
So let's go run that.
We've got our listing at the top.
Now as we're viewing things, Goonies got skipped, but it says Goonies led to 10 pretzel carbs
being consumed.
Goldfinger, well, he tallied up some nachos, as did Ghostbusters.
And then we see our pretzel down here.
So we've got all of our candies being randomly selected, or all of our snacks being randomly
selected when the movie's viewed.
Okay, now we teased you at the beginning of this section by talking about a treasure hunt.
Well, now you get to introduce that concept in your game.
Each time a player takes a turn, they'll find a random treasure.
For now, you'll just print out the random treasure that was found.
And in the next section, we'll talk about using hashes to accumulate the treasures for
each player.
So come on back when you're done.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.