Learning Ruby: The Object Model
Object-Oriented Programming(OOP) is the essence of Ruby, and as such, it’s hard to really elaborate what an object is. At first, it was hard for me to grasp what an object was, let alone the concept of OOP, but with more repetition, exposure and practice, it got easier and easier to understand what OOP was. Now the challenge is to teach what I’ve learned. In this article, it is my goal to explain what OOP is to an absolute Ruby beginner, to explain it in a short and concise way that I could have understand it a couple weeks ago when I was struggling with the concepts; and through teaching it to you, I hope to reinforce my knowledge on the subject as well. The best way I learn, and the best way I know how to teach someone is through examples and visuals.
First of all, though the concepts here will be written for Ruby, through my experiences with practicing JavaScript the last couple days, the concept of OOP also applies to JavaScript, and I would assume to very many other languages.
What Are Objects?
Throughout tutorials and readings on Ruby, you’ll hear that everything in Ruby is an object. I don’t mean to be repetitive and reiterate that concept but it is. Everything in Ruby, just like in real life, is an object. Now you’re probably like, “Yea Bao, but what exactly does that mean? What, in laymen’s terms, is an object?” In terminology that I would probably use to try and explain this concept to someone in person, an object is a singular entity with qualities or attributes that are unique to it (also to note, these qualities or attributes are Objects themselves. Talk about object-ception).
For example, humans are objects with attributes. Each object, when created, has a unique set of attributes. In the real world, when a baby is born, they have attributes such as a name, eye color, hair color, height, weight, number of fingers, etc. Same goes for a code example:
class Person attr_reader :name, :eye_color, :hair_color def initialize(name, eye_color, hair_color) @name = name @eye_color = eye_color @hair_color = hair_color end def greet print "Hello!" end def run # Some code for running end def work # Some code for working end end
In the code above, a class was created called Person. This person class, when created immediately receives a name, eye color, hair color, and also has the ability to greet, run and work. And each time you create a new “Person,” you’ve created a new Object that has a name, eye color, etc. So let’s create a new person, we’ll call him Bob.
person1 = Person.new("Bob", "Brown", "Black")
When we type that line into the console, the object for Bob, is stored in person1. And when we check in the console/terminal for what person1 is, we see that person1 is named “Bob”, has “Brown” eyes and “Black” hair.
[2] pry(main)> person1 = Person.new("Bob", "Brown", "Black") => #<Person:0x007fe2aba7b170 @eye_color="Brown", @hair_color="Black", @name="Bob">
Let’s create a new person. We’ll call her person2 for simplicity.
[3] pry(main)> person2 = Person.new("Susan", "Blue", "Brown") => #<Person:0x007fe2ac0ecc98 @eye_color="Blue", @hair_color="Brown", @name="Susan">
What we have now is a new Object. That object is stored as the variable person2 and has the name “Susan.” She has “Blue” eyes and “Brown” hair. Take a look at the graphic below:
Each time you create a new instance from the Person class, you created a new object. We have two primary objects above, labeled person1 and person2 like before. Each person/object in the diagram has attributes that are unique to it that we initialized when we created that instance of the Person. In order to access these attributes, you simply type: person1.name to access “Bob” or person2.eye_color to see what “Susan’s” eye color is (blue). I hope that this diagram helps you understand what object is, this is was the point in which it clicked for me.
Now that you have a better idea of what an object is, I just wanted to quickly re-address the concept I mentioned previously of “object-ception”, (self-named, it’s not a technical term). In the case of person2, not only is person2 an object from the Person class, but person2’s name, “Susan,” is an object from the Ruby default String class. If this is a bit confusing for you, no worries, you can ignore this paragraph and revisit it later after a bit more practice and exposure to objects.
In the next post we’ll take a look at examples of how Ruby, as an OOP language, centers around these objects.