Object Oriented Programming (OOP) in Ruby: What is it Exactly?

  1. What are the four pillars of OOP?
  2. How do you implement OOP in Ruby?
  3. What is the importance of using OOP?

What Is Object Oriented Programming?

Events are something you’ll come across when working with UI /FrontEnd frameworks…

What Are The Four Pillars of OOP?

Encapsulation

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:

  1. put car key in ignition
  2. turn key clockwise until engine starts
A method encapsulating individual pieces of code that deal with the action of starting a car.

Abstraction

Abstract art for an abstract topic | Painter: Wassily Kandinsky
Although this image is for OOP in Python, Classes and Objects operate the same as in Ruby

Inheritance

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.

Image from geeksforgeeks.org

Polymorphism

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.

Polymorphism is essentially creating something that can have multiple uses(implementations)

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.

Summary

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…

A coding and motorcycle enthusiast who’s passionate about personal development and learning. Recent Flatiron School of Software Engineering graduate.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store