Meet Ruby / Everything is an Object
Subtitles of the Movie
In this video we're going to take a look at why everything is an object, especially to Ruby. Now, if you're used to a modern operating system, you're used to dealing with the paradigm of the desktop that we're seeing here with Windows, hard drives, files, individual files, that type of things, and, and they're very connected to our everyday experience of an object. So on my desk I have things. I have pens and pencils and books and cups and the computer, and they're all objects I can touch, and I use, on the computer, on the operating system, I use the mouse to touch the various things. Well, in programming there's a particular paradigm of programming called object-oriented programming, and your approach to programming is to treat all the things of your program as objects. And its, can be a little bit daunting to kind of understand that concept and interact with it and program to it. So I'm going to use a tool called Interactive ah, Ruby, IRB, it comes when you install Ruby and we cover installation in other, videos, so you launch IRB by just typing that, and I can interact directly with the Ruby interpreter right now, and if I enter something, such as a number, it comes back and says, Oh, one, there you go. If I say, one dot class, so, here what I'm trying to show you is that to Ruby, everything is an object and and, that's pretty unique because in a lot of languages you still have things called primitives, so a number is just literally that; it has it's value and it's a number, and if you were to look in memory for that number stored, you'd see that number in binary. That's not the case with Ruby. Even this one is a, is an object and it has a class, and in our case here, it's a fixed num. And, and in a class essentially means that it's the template that this object was created from. And, and we'll talk about that further in other videos, but I'm just introducing the concept to you. So, everything has a class. If I type in um, an A, let's ask it's class. That's a stream. If we type in um, something like this, that's an array it's telling us. And we can do stuff with that array. We can say, oh, hey, sort that array, or um, better yet, let's reorder this. Let's make it two, and tell that to sort. And now you can see it's sorted at four. So it has a, a definite class to it. Now the paradigm in here is that you have some object and it's really this is the reference to the object, it's not actually the object, that's the hard thing for people to kind of get, is that this is like the name. In other words, like my name is Al, well that just points to me. That isn't me; my name isn't me, it's just my name; it's how people communicate with me and that's what this paradigm is here. This is just the name of the object, it isn't actually the object, it just points to it this is in memory. And then you give it, if you're going to do something with it, you're going to give it some method. And a method is essentially like a little function, or a mini-program if you will, that's going to run by that object. And each class has it's own set of methods and we'll see that as we go into Ruby more and more, the various methods that are available out there. So if we wanted to look at a particular object; let me create a variable that holds a string. So we're going to say X equals A, and it'll come back and say, okay. So if I ask for X it's just going to print out that; if I say X dot class, what do you think it's going to print out? It should say string, because that's what A is. Sure enough, there it is. It's a string. We can also ask for where it's actually located or what it's object ID is, and that number essentially has to do where it's in memory and with the interpreter, and all those kinds of things, and it's not real germane what that specific number is, but you should realize it's unique. Let's do another one. There's Y, and let's get it's object ID now. And you see it's different. Now X equal Y, let's, let's ask it that. That's true. But X is not the same as Y. Even though they're equal they're not the same thing. But if we say Y equals X now let's ask what Y's object ID is. You see, now it's the same because it points to the same object. So if I ask for Y right now it should be that A and let's say X equals B now. So X is B, what should Y be? It's still going to be the A because it's pointing to that old object. We repointed X to a new object. So this object-oriented stuff gets a little bit hard to kind of wrap your head around, but the main point here is that everything in Ruby is an object. There's no distinguishing between things that aren't an object and things that are an object. And everything has a class. Everything has a class. Like we showed you, if we do the one, it's a fixed number. If I say X equals 1 now X's class is going to be fixed num. What's Y going to be? It's still pointing to that A unless we say Y equals; let's ask Y it's class. It's a string. Now let's say Y equals X, now what is Y's class? It's going to be fixed num because Y now points to that same object. So here, this is just representing the Y and the X are just pointers to the actual object wherever it is in memory, and you can mess with that. So, again, the main point to come away from with this video is that everything in Ruby, absolutely everything, is an object. And an object gets created from a class. The class is essentially the template from which objects are created, and you can create classes and that's how you program is in Ruby is creating classes and then what's called instantiate them, or create an object from a class, and that's what a lot of the rest of the video series is about. It's about creating your own objects and doing different things with, with those objects.
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
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 