More OOP / Advanced Techniques
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to take a look at some more advanced attributes of using your own classes. So in prior video, we created the card and the deck classes to essentially represent a deck of cards in a computer program. So we're going to go ahead and implement this and play around with it a little bit. So I launched into IRB and these files, it's called, that this one is deck of cards here, is cards dot rb in the working files of, it'll be under Ruby 19. So if we launch into IRB, first thing we got to do is load our card file so we can get access to these two classes. And the way this is structured, we actually don't access or create the card class. We create a deck class and that creates the cards for us to play around with. So let's, let's get a deck of cards by issuing and we'll just give it that. And it doesn't like that and I'll show you why here. Because it's expecting the value to come in here for empty. Now, there might be a better way to implement this. I'm not, I'm not going to dwell on it a whole lot of time, what that could be. I'm just going to say, I'm going to use the word full and that for sure is going to let us get a full set of cards. If I put empty here, I'll show you in a second, then it's going to get us an empty deck. So let's issue this. That gets us a full deck of cards. So if we go in here and we can get access to the deck via this attribute access or deck. So let's go in here and say deck dot deck, get the, let's get the size of it. It should be 52 cards. Can see the first one in there and we can see the last one. And you can actually see it up here too. If we treat this like a deck that's sitting on a table that we're playing with, it's essentially going to represent a stack. In other words, the last card on is the first one off. So you're going to use push and pop to get things off. Now, I've already got a draw on here so if I issue deck dot draw, it's going to give me the last card, which is that spade. And now if we go and print the last one, it'll be the next card. Now, the problem with that is I just lost access to that card because I didn't actually, you know, get a, get an object to represent it so it's gone at this moment. So if I was developing a program, I'd have to make sure that I had a structure to hold that temporary card as I drew it here because as I continue to draw, I'm going to lose reference to those individual card objects because right now all it did was say hey, here you go and then because I didn't have anything that I was assigning this here to, once it did this, the reference is gone. I can not get back to that card. So if I was going to take this further into the game, I'd have to keep that in mind. Now, let's create a discard pile and we'll just say deck dot new and this is where I'm going to put in here and put my empty. Now that's going to get me an empty deck. Now if I do this, I'm going to just deck dot draw. And it's actually going to move the card from my deck of cards onto the discard pile. So let's look at the deck of cards and you can see that now the last card is the queen of spades and what do I have in discard pile if I print deck? You'll see what's in there. It's king of spades. Let's run this again. Now I have a king of spades, queen of spades on there and what's my last one on, on the deck? Now it's the jack. You can see I'm working up through the order there. I'm going to draw one more time because I want to show you the shuffle here. Let's do two more. Ok. So now let's do the discard print. So I've got, I've got four cards on there. Now we can shuffle that. And you can see it reordered that. No, it didn't reorder it. Yeah, it did. I was getting kind of surprised. There was probably not enough cards to randomize it real well, but it did a little bit of reordering. Let's try another shuffle. There we go. We're getting a little bit. So you can see that shuffling, you'd have to do it a few times and four cards is probably not a good example for enough randomization to occur with it. So now we've implemented this stuff up. Now, one of the things I'm looking at here is that I don't like the fact that I have to do this to access or to push a card onto my deck, I have to essentially say right here. I don't like to access this deck object here. Instead what I'd like to do is create a new method and call it place and then we go deck and this is the final thing I want to show. This is, this is the cool part of Ruby being an interpretive language. So I'm going to reload the classes and now both of those objects have that attribute or that method automatically. So I can go in here and if I go up here and instead of calling this, I'm going to say place and make it a method call, and there we go. Now we have this method. What I did there was I actually updated my class, I added a new method and I just reloaded the class definition and the objects that I had declared on that of that class, the deck class, automatically have that new method in them. And that's the awesome part of an interpretive language, especially something like Ruby to be able to do that. So that's some more advanced concepts of object-oriented programming.
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 