Introduction to OOP / Program Flow
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 look a little bit at flow control, or program flow, in other words, how does the program actually get executed in Ruby. So we're going to keep on using our little book class that we've used in previous videos here, and we're going to use IRB from the command line in a second. So just to review, this is our book class and I've actually changed it a little bit. I've added two things down here. I've added this put hello, and I added another class declaration on here called Magazine. So let's go ahead and launch IRB and load that file. And I wanted to point out something here. If I just put load without a file name it's going to give me an error. I've got to make sure if I'm using load to put the whole file name in there and you can put a path in there, too. It, it works. So I'm going to hit enter here, and think about a second, just looking at this, what's going to happen. And make a guess and then I'll hit enter here and we'll see what happens. Hopefully you've got your guess now. Well there you go. It's obvious it executed this put statement right here. So if we think about it, it must have gone through, looked at these declarations, accepted those, hit this one, done that one, and then gone down here and accepted that class declaration. So let's make sure. Let's go in here and see if we can do um, declare Magazine, which would be a new method. We've, we've been doing book the whole time here, but let's see if we can do this. Sure enough. It, it recognizes that there's a class that we've declared down here and we can use it. Now can we access this PutS method? And we know it got executed one time and, and it's set up. So there, there may be instances where you'll find this variable, but the way to think about how this gets loaded up is that these class declarations get added into essentially the object tree of the current Ruby Interactive shell that's running, if that, that's the case, or the current Ruby session if it's like a program that you're running and you include other things in it. So once it gets in that declaration, it hits this next statement, it just executes it and then it continues on. Well, this one, it doesn't, there's nothing here. It's not going to care about this after it executes it, it just does it. Whereas these, it actually adds those into its little class hierarchy of what's going on. So let's, let's create a new file here, and we'll just, we'll just say in here, let's do a load book dot RB, and then, oop, I've got the wrong parenthesis, there we go. And then let's say, B equal new book dot new, and then b dot, get my right things here. We'll just do something simple here, test, you know, I'm going to save this out here. Let's just call it test out ruby. Make it easy. And now I'm going to run over to my, I'm going to exit out of here and make sure the file's there, and there it is. Now we'll just do Ruby and then test, and see what happens. And, we've got that hello, we got that to come up, isn't that interesting. But let's, let's do this. Let's add one thing in here, and let's do a puts B dot get I-var. Let's run it again here. Now we're getting more behavior. We get that test coming out. So it's apparent what's happening here, or maybe it's not, I guess that's my job to explain it. Let me shrink this up a little bit so we can see these screens here. So, I'm running this program right here. I've asked it via this terminal to run. So it goes ahead and the first thing it does is it loads up this file, which is down here. So it loads up these declarations, it hits that statement there, goes ahead and prints out hello, we get that, then we set that I-var and then, well, actually we instantiate a new book before we set the I-var, and then we set this I-var to this value of test and then we go ahead and get the I-var, and then when we are getting that value, then we turn around and put that into this puts method call and that prints it out. So you can see here how that program flow works. So, when we actually did the load here, it executes this. Now, just to be sure that you believe me here, let me go and comment these out, so the only thing that happens is this load, and I'm going to go ahead and rerun it, and there's my hello. So you see it didn't depend on this because that's up here. Let's go here and just, let's just see what this is going to do, and just play around with it a little bit and see if it does anything. Now, you see it went through, executed that, and continues on. Now let's go and do a new one there. See if it's, now, I guess my question would be, Are you thinking that it's going to execute once or twice? Let's give it a try. Just once. So that new simply goes out, gets these class attributes, method call, and so forth, and creates an object from there. So that's a little bit about program flow.
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 