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
Okay, welcome back.
We want to talk a little bit about design in this section.
Yeah, I was looking at our playlist class, and it currently has a little bit too much
responsibility, so let's change that around a little bit.
Okay.
In the play method here, we see we're looping through all the movies, but we're also rolling
this die, and then we're either thumbs-upping or thumbs-down the movie.
This doesn't feel like it should be the responsibility of a playlist to do.
It should just be able to play the movies.
Right.
And in the same way, the play method in your game is probably doing too much.
So I guess the question is, who's responsible for this logic?
It doesn't really make sense to add it to the movie class, because a movie's not going
to review itself.
So we need something that encapsulates the concept of a reviewer of some sort.
Yeah, we need a reviewer.
Hey, how about we get Waldorf and Statler to review our movies?
You know them.
They're from The Muppets.
Oh, those guys would be perfect, because they're cranky and cantankerous both.
Yeah.
So let's have them responsible for reviewing movies for us.
Okay, so if we give them a responsibility of thumbs-up and thumbs-down, we could use
a class, but we don't have any objects.
Well, and I don't think a reviewer is going to have any state initially either.
We just basically need a method that takes a movie, and then it will review that.
Right.
So instead of a class, let's use a module.
Let's do that.
So I'll just create a file inside of here.
I like that name, Waldorf and Statler.
So we'll just call the file Waldorf and Statler, like that.
And then inside of here, we want to define a module.
Now a module is like a class, but we start it with the module keyword, and it's going
to be the Waldorf and Statler module.
Now notice the naming here.
We use this camel case for the actual name of the module, but then the file name, we
separate the words with underscores.
So that's just a convention in Ruby.
And then we can end the module just like that.
So unlike classes, you don't instantiate modules.
In fact, you can't instantiate modules.
If we try to instantiate this now and call the new method to get an object out of here,
well we get this error, undefined method new for a module.
So what's a module good for?
Well later we'll use modules as mix-ins and namespaces, but first we'll just use a module
as a bucket of related methods.
So let's add a method in here.
We'll call it review, and I'll just print out bravo, just like that.
But we know that this is an instance method.
If we were to have this method inside of a class, we would need an instance of the class
to actually call that method.
So there's no way to call this method right now because we can't create an instance of
the module.
So instead we need to make this method into what's called a module method.
And we do that using the self variable.
Self.review creates this method on self, and in this particular context, self is the module.
So the way we call this method is we can just use the module down here and call the method
directly.
So we could just call review, just like that.
We don't need an object, don't need to call new at all.
It's just a module level method that we can call from the module.
If we run that, sure enough, we get bravo.
But there's just one problem.
Waldorf and Statler, they would never say bravo.
Yeah, that's entirely too positive for them to say.
So I guess we need to change this around to actually do something with our movies now.
So let's go back over to our playlist class.
And what we want to do is basically move this code over into there.
So I'm just going to take, let's see, let's take this code over.
Actually, we want to leave the put us at the bottom.
So let's take this code and we want to go back over into our module.
And inside of review, we'll just paste that code in.
We definitely need to pass a movie into this method because we use a movie down here.
Oh, and we also have this method roll die.
So let's go back over to our playlist and grab that as well.
Because that's how Waldorf and Statler are actually going to review the movies.
They're just going to roll this die.
Well, unless Miss Piggy's in it.
Well, that could be kind of fun.
Now this method roll die here has to be a module method as well, because we're going
to call it inside of this module method called roll die.
So we need to use the self.syntax here too to make that a module method.
So we've got our movie.
We've got our number roll variable.
The rest of this code stays exactly the same.
We'll save it.
We need to go back to our playlist.
Now right here where we took out that code, actually it's inside the loop for movies,
we want to call our Waldorf and Statler module.
We can call our module method review.
We want to pass in the movie.
And remember, now we have this dependency on this module.
So at the top of the file, we need to require relative the Waldorf and Statler module.
Just like that.
So now let's go run our flix.rb file and just run that.
Ooh, we have an error here.
On method review, we're taking a movie in there, but right down here at the bottom,
I left in this call to Waldorf and Statler.review.
Remember when we require this file, it's going to run all the code.
So we've got to take that little bit of example code out of the bottom.
Just double check our playlist.
Yep, it's calling review, passing in a movie.
So we should be good now.
So let's run it again.
We've got our two playlists, and it looks basically the same as it did before.
The only difference is we've moved the responsibility of who reviews this movie over to the module.
From this outside, we're getting the same results.
So let's check our specs now.
If we go over to our playlists spec.rb, let's just go ahead and run that.
Ooh, now we get a problem.
And if we look through here, we kind of notice that it's looking sort of like random again.
The first one failed and the second two of them succeeded.
If I run it again, well, they all failed this time.
So what's going on there?
Well, it sounds like we're getting this random behavior out of a die again.
Let's look at our spec.
Oh, remember, we were stubbing out this call to Roll Die, and we were stubbing it out on
the playlist object here.
Well, we don't want to do that anymore because we've moved the Roll Die method over into
the Waldorf and Statler module.
That's right.
So if we're going to stub it, we've got to stub it in the right place.
I'm just going to take that.
We'll put it down here.
And I think there's one more spot right here where we call it.
Paste that in.
And now we should be able to run this.
Sure enough, all of our tests are now passing.
So we've successfully done the refactoring, moved the code over, and then we run our tests
at the end, make sure they all pass so that we know we didn't break anything.
Now we did a lot of refactoring in this section, so should we run all of our tests?
That's actually a really good idea.
Let me just go back over to the command line and we can run all the specs.
We've got a couple of them in here, rspec dot, and we'll add in some color.
And sure enough, we're all good.
All right.
Now in the exercise, you're going to get to do a similar refactoring with the game.
It'll be your chance to write your first Ruby module.
Now we haven't seen the last of modules.
Now we're going to use modules a little later on as mix-ins, a unique way to share code
in Ruby.
We're also going to use modules as a way of namespacing our code.
But for now, just think of modules as buckets of methods.
So give this exercise a go, and then when you come back, we're going to dive deeper
into blocks and iterators.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.