Object Oriented Programming (OOP) in Ruby: What is it Exactly?
As a programmer, object oriented programming (OOP) is a term that you are bound to hear often as you progress and grow as a developer. If you are new to coding or just need a refresher on the topic, then you’ve come to the right place. We’ll discuss four main questions concerning the subject:
- What is Object Oriented Programming?
- What are the four pillars of OOP?
- How do you implement OOP in Ruby?
- What is the importance of using OOP?
Please keep in mind that while we will be focusing on object oriented programming in ruby, the general concepts behind the points discussed can be applied to any OOP language. Let’s begin shall we?
What Is Object Oriented Programming?
Put simply, Object Oriented Programming (or OOP for short) is the process of abstracting correlating variables(data) and methods(functions) into a thing we call an object. Anything in your code that holds information and has behavior is an object. And all objects belong to what we call a class. A class is like a blueprint or a template for how an object is created. You may not have realized it, but if you have done any coding thus-far, then you have most likely been introduced to some of Ruby’s built-in objects. For example, Arrays are objects because they have properties like length that tell you how many elements are in that array and it also has certain behaviors that we can call on through methods like .first, .last, .pop, .etc that cause an array to perform different behaviors such as adding and removing elements. Other data types like strings, numbers and hashes are also objects as well.
Still confused huh? Well,…think about it like this: Imagine you have a car factory. The car factory would be a class and each individual car the factory produces would be an object. That object or car in our analogy has properties such as color, make, price, and model. A car also has actions or behaviors like start engine, stop engine, brake, park, and drive. In a similar sense, when we are creating objects in programming, we are building things that have both properties and behaviors. And essentially, these objects represent real world things. To go along with our car example, If you were building a car racing mobile app game, you would create a Car class that would contain various properties/variables and characteristics/methods that would represent a car.
What Are The Four Pillars of OOP?
Now that we have an idea of what the heck Object Oriented Programming is, we can now focus on the main notions that make up OOP. We call this the four pillars of OOP, which are: encapsulation, abstraction, inheritance, and polymorphism. Wow, that sounds complex…but no need to fear, we’re gonna break down each pillar individually. Easy peasy.
The first pillar we’re gonna break down is encapsulation. If you have already written methods (also called functions in some languages) before, then you have already used encapsulation. When you write a method, you are grouping — a.k.a. encapsulating — related pieces of code that are meant to perform a specific task. For example, you could break the process of starting your car into a series of steps:
- take out car key
- put car key in ignition
- turn key clockwise until engine starts
These are all individual actions that we encapsulate into one general idea, ‘starting our car’. Likewise, in programming, we encapsulate similar pieces of code into a method. E.g., our start_car method below encapsulates code related to the process of starting a car. Encapsulation in OOP operates the exact same way…an object encapsulates related variables and the methods that make up what an object is and what it does.
Take a look at the artwork above. What would you say is the meaning behind the strokes, and the color palette? What is the artist trying to convey? I’m gonna be honest, I have no clue whatsoever… there’s so much detail and information for my brain to process to the point I have no idea what the artist is wanting to express. What the painter is trying to convey is really up to my discretion.
With coding being an art form itself, it can be very much the same. However, unlike actual art, as coders, we don’t want to leave our fellow coding brethren with no clue what pieces of our code(art) means and what it does. It’s important for our code to be able to be clearly understood and interpreted by others. This is one of the main reasons the pioneers of computer science came up with the beautiful idea of abstraction. (P.S. not saying I don’t like the artwork, I just don’t understand it, lol)
According to computersciencewiki.org, “…abstraction is a technique for arranging complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level.”
What this means in our sense of the word is that we’re taking something complex and removing all the meticulous details to make the idea more simple and understandable. We can also call this process “generalization” because we’re taking something specific and making it more widely applicable.
From Computer Science Wiki In software engineering and computer science, abstraction is a technique for arranging…
We use abstraction on a daily basis without even realizing it. For example, if someone asked you what are your plans for the weekend, you may say “ I’m going bowling with some family and friends.” Family and friends would be the abstraction. Instead of saying, “I’m going bowling with my cousin Tim, my mom Martha, my dad Joe, my sister Lisa, and my two best friends from college Raymond and Chris”, you simply abstract the details into the words friends and family. ABSTRACTION IS EVERYWHERE…
A perfect coding example of abstraction would be the use of methods. We take the details of code and abstract it into a method that help our colleagues better understand what our code is doing and why it’s there. This also makes our code easier to debug when something breaks, and super easy to make changes when necessary (our code is more adaptable). What does abstraction look like in OOP though?
We use a thing called classes in Ruby to generalize our objects. Classes are basically a generic blueprint or template for each instance of an object that is created. To explain what this means, we’ll continue with our previous car analogy. Like the image above illustrates, Lamborghinis, Ferraris, and Teslas(Electric) are all different makes and types of cars. But take — abstract — away all the specifics of what make each one unique and you’ll see that they are all in general just a car. So to go back to the idea of creating a car app, if you wanted to create each one of these cars in your app, instead of going through the tedious, and repetitive work of writing near identical code for each individual car object, we simply abstract our code into what we would call a car class, which would act as a starting template for each specific car object we want to build.
Inheritance is another way in which OOP helps use avoid writing repetitive code. To keep it plain and simple, inheritance is the process in which one class can require or inherit the properties and methods of another class. When this happens, we refer to the class that inherits from the other as the child class and the donor as the parent class, respectively. E.g. a car class and a truck class could inherit from the parent class of vehicle, allowing each one to have access to certain methods like: start, stop, park, honk horn, etc. The child classes would also have access to parent class’ variables like make, model and price. This again helps us to stay away from writing repetitive code.
Polymorphism is actually a combination of two words; Poly(meaning many) and morph(meaning form), So in plain english, polymorphism translates to “many forms”. One way we display polymorphism is through inheritance.
What is the importance of using OOP?
OOP allows you to make more abstracted and easier to manage code base through the use of objects and classes. While not the only accepted method of programming, OOP programming is widely accepted and encouraged in the tech industry do to its abstract-ability, ease of use and conventionality. If you want to dive deep into the realm of programming, then OOP is some of the necessary diving gear you’ll need to make the plunge.
Congrats! You now know what object oriented programming is! We’ve also discussed the four pillars of OOP and the importance of using object oriented programming. In the next article, I’ll cover a coding walkthrough that shows the OOP in Ruby is used in your text editor. I hope that you enjoyed reading this blog and I wish the best on your coding journey! Feel free to check out the video below if you still want to know more…