Object Oriented Programming with Ruby

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs.

The basic programming concepts in OOP are:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

Abstraction

The first principle of OOP we are going to cover is data abstraction. Data abstraction is just a fancy way of saying that we are going to model our data to look like it’s real life equivalent. So, how do we model a drink? Well a drink should be an object that has a name, size, ingredients, alcoholic constant. It should also be able to give details about itself. Translating this into Ruby and we get something like this.

This is what is called a class. A class is just a blueprint that is use to create objects that represent some structured data which in this case is a drink.

Encapsulation

Wrapping up the data into a single unit is known as Encapsulation. It can also be called as the procedure to bind code and the data which is affected by the code together. By applying encapsulation you can protect your data from being manipulated by another source. In simpler words, it can be considered as the mechanism which allows data only to be manipulated by the member functions of the class in which they are declared.

You can achieve encapsulation by declaring all the variables of class as private( they are by default private) and all the member functions as public(they are by default public). Now, these variables will only be accessed by these public methods of the class. Let’s say that we have two new drinks. martini and macchiato as mach .

Here the instances of class “Drink” are martini and mach.

in the above code you can observe that the member functions and data are only accessible through the object or instance of the class.

for example; in pry,

martini.name will give us “Dirty Martini” not for the mach’s name “Macchiato”

Inheritance

Inheritance is a relation between two classes. We know that all cats are mammals, and all mammals are animals. The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own. If all mammals breathe, then all cats breathe.

http://rubylearning.com/satishtalim/ruby_inheritance.html

Though we didn’t specify how a Cat should breathe, every cat will inherit that behavior from the Mammal class since Cat was defined as a subclass of Mammal. (In OO terminology, the smaller class is a subclass and the larger class is a super-class. The subclass is sometimes also known as a derived or child class and the super-class as base or parent class). Hence from a programmer’s standpoint, cats get the ability to breathe for free; after we add a speak method, our cats can both breathe and speak.

Polymorphism

Polymorphism is the ability for different types of data to respond to a common interface.

Here’s a simple example in Ruby with inheritance :

https://devblast.com/b/ruby-inheritance-encapsulation-polymorphism
https://devblast.com/b/ruby-inheritance-encapsulation-polymorphism

As you can see, we sent the same message to different object and got different result. The print method is a single interface to entities of different types : XmlDocument and HtmlDocument.

References

Software Engineer

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