Ruby is a genuine Object Oriented Programming language, arguably one of the most powerful OO languages. So, what is the big deal about objects? Why are they important?
At its core, object orientation is a logical abstraction. Humans naturally think in terms of objects with attributes and behaviors. These objects interact with each other and with us. Abstraction is a common theme throughout computer science. It ensures that as users, we're far removed from what's happening "under the hood". A simple example of this is the combustible engine.
As the driver, we are abstracted away from the inner workings of the vehicle we are driving. How the pistons or crankshaft operate and the function of the spark plugs are not concerns of the average driver. The driver is removed from these lower level technical interactions and instead possesses a higher level but less technical understanding. As drivers we know there needs to be gas in the car and then the accelerator peddle makes the car move forward. The intricate details are hidden away from us. Abstraction is the hiding of such details. It will come up again as it plays a crucial role in OOP.
Experience in learning and teaching has led me to believe that bringing up objects in chapter one is a mistake. It is too subtle a topic for beginners to firmly grasp. Learning about objects in Ruby before having laid a proper foundation is an exercise in futility. Take a moment to review the information in the previous chapters. It is important to understand variables, data types, and arithmetic operators before moving on. Beginners tend to accumulate partial and incomplete knowledge, filling the gaps with assumptions or erroneous information. They continue learning with the hope of filling in those gaps as they go, unaware of the accumulative nature of computer science. Unfortunately, these gaps in knowledge are more likely to begin compounding as you move toward more advanced material. The little things you didn't bother learning will manifest themselves in your work and eventually halt your learning.
Oriented around Objects
To "orient" means to align or position something in a relative way. It is the "object" that Ruby is oriented around. Objects are the heart of Ruby. We are going to construct our applications by modeling objects, similar to objects in the real world. A friend of mine that shows dogs entered The 141st Westminster Kennel Club Dog Show. "What is a wonderful opportunity to discuss objects in Ruby" - I thought to myself.
Let's pretend we've been employed to create the next Westminster mobile app. A reasonable approach to developing this app is to determine what our data models will look like. Since Westminster is a dog show, we can assume we'll have a "dog" object, and in fact, we'll have many of them. We'll end up with a large list of dogs, for example, Bulldogs, Chow Chows, Dalmatians, Poodles, Bichons, and Boston Terriers will all be represented as objects in our application. All of these dogs belong to a special class, called "Non-Sporting".
Classes are another important OO concept. They provide a container for related objects, like our "Non-Sporting" class. Our application would have other classes as well: Herding, Sporting, Hound, Working, Terrier, and Toy. Each class is a container for different breeds of dogs that share similar attributes. By separating the different breeds of dogs into different classes, judges are able to compare them and declare a winner. In this same respect, we'll model this and other applications by making logical connections to real world scenarios.
An object is characterized as having data, attributes, and behavior. The behavior is the result of responding to messages. A Ford Mustang is another example of an object. It belongs to a larger class of 'cars'. It has attributes associated with it such as engine size and number of doors. It also has some behavior associated with it, chiefly driving forward.
From the Real World
Examples are great to introduce new material but insufficient in providing a complete understanding. We need to look at a real application. It's okay if you don't understand everything right away. The purpose is only to familiarize you with the concept of objects. The following is a screenshot from an application in development.
The first line creates a class named "User". Lines 11 through 16 define some of the attributes that will be saved with the user. As discussed previously, the class will function as a container, housing the properties of our related user objects. The user objects will have their attributes stored in a database. This application uses MongoDB. The User class outlines what information to collect on each object, starting with line 11 we'll collect, email, password, username, user id, provider, and image. Let's create a new user with the username, "CoolDude17". We create the user and persist the data to MongoDB. CoolDude17 is an object. CoolDude17 is an instance of User class. If we create another user with an email, password, and username of "CheerChick1997", we now have another instance of the User class. CoolDude17 and CheerChick1997 are both objects, instances of our User class.
By thinking in terms of objects and modeling our application this way we can take full advantage of the benefits of OO design. Notice the close relationship between class, object, and instances. This is vital to understanding how to develop applications in Ruby.