- We're going to begin with the most powerful and important concept in all of object oriented programming, and that is classes, and especially, how do you define your own classes. That's what we'll look at in this movie. In case you don't already know, a class is code that's grouped together by a common theme or a common purpose that provides a template of the state and behavior of objects created from it. Notice that last part of the definition. We're going to have a template and we're going to use that template to create objects. We've already worked with classes in basic Ruby.
Integer, string, array, these are all examples of classes. Even true, false and nil are classes. You can always ask Ruby to tell you what a class is, by taking the object and then asking for the class method on it. So for example, if I have the string pasta, and then I called dot class on it, Ruby will tell me what class that is. String, array and hash are examples of built-in Ruby classes. What we want to do, is to be able to define our own classes. We can do that by using the class keyword, followed by the name of the class, and then the class definition, and then end at the very end of it, so that everything from class to end, is the definition of our class.
When you define a class, you should use singular nouns for the class names, for example, shopping cart, and you want to capitalize and camel case the names. So, you can see when I have shopping cart, I start it with a capital S, and then I don't have a space or underscore or anything to separate shopping and cart. I run them all together, but I use a capital C. That's called, camel case, it's like the hump of a camel. You also want to define each class in a separate file that helps you keep them organized, and you also typically group your class files all into a directory so that you can easily find them, and they don't get mixed in with your other code.
Classes in Ruby can be redefined or modified without error. That's not true in all programming languages, but Ruby lets us re-open a class, make changes, add new behaviors with no problem. So, you'll remember, in the definition, there was that line that said that a class "is a template of the state and behavior of objects." So when we talk about classes, the attributes of the class are going to be what hold the state. Attributes will keep track of what is the current state of that object. And then, methods, similar to the custom methods that we worked on in basic Ruby, are going to define the behavior of that class.
We'll be talking a lot more about attributes and methods later in this chapter. The way that you add attributes and methods to your class is simply to define them as custom methods inside the class definition. So instead of having a stand-alone custom method, you now have a method that's part of your class template. So, in a location that you can find and work with, I've chosen my documents folder, you want to create a new folder, I've called it Ruby underscore Project, it's just an empty folder right now. I'm going to go ahead and add another folder inside of there, so I'll create a new folder, and I'll call this one Classes.
I'm going to drag that in my Ruby Project, there it is. Now it's empty, so I'm just going to save that first file as in documents, Ruby Project, in Classes, and I'm going to call it, person singular dot rb. Alright, so you should have something that looks like this. Person inside of Classes. Now I'm ready to make my definition. I'll just make this a little smaller. So all you need to do is put class and then the name of the class, which is going to be a singular noun, upper case and camel case.
So, Person, it's just one word so there's no need for camel case here, but if I was doing, let's say, a checking account or something, I might have Personal Checking, right, something like that, but I'm just going to have person, and then I'm just going to come down here and have end. I'm not going to put any definition in there now, I just want to have the beginning and end, and I'll save it. Now this is a properly defined Ruby class. One more thing, though. You may remember that in the basics, we learned that it was a good habit to always put a shebang line at the top, something that looks like this, and then it says where to find Ruby.
We're not going to do that in these class files, and the reason why is we're going to have other script files that will use that, and those files are going to load this file in to work with it. So they will already have told the command line that we're using Ruby and everything long before it actually gets to this file, so it's not necessary to put it in every single one of your Ruby files, just in your main script file. So with that, we now have our first class defined.
- Defining a custom class
- Working with instances of a class
- Class and instance attributes and methods
- Class inheritance
- Working with dates and times
- Using modules as namespaces and mixins
- Raising and handling exceptions