Ruby 101: Object Oriented Programming part 2

TK
The Renaissance Developer
3 min readMay 21, 2017

Part IV: Encapsulation, Inheritance, Module

It is the fourth part of Beautiful Ruby series by Tk. If you are a totally beginner developer learning Ruby, you should start with part I & part II of this series. The first part we learn about the history about Ruby & very basic stuff like variables, control flow (if-else statements) and looping / iterator. For the second part we talk more about Ruby Data Structures, how it works & how we can iterate through it.

I also advice you to read the first part of Object Oriented Programming with Ruby

Let’s start! :)

Encapsulation: hiding information

Encapsulation is used as a mechanism to restrict direct access to objects’ data and methods. But the same time it facilitates operation on that data (objects’ methods).

Encapsulation can be used to hide data members and members function. Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition. — Wikipedia

So all internal representation of an object is hidden from the outside, only the object can interact with its internal data.

In Ruby we use methods to direct access data. Let’s see an example:

We just implemented this Person class. And as we learnt in Ruby OOP part 1, to create the object person, we use the new method and pass the parameters.

So I created me! :) The tk object! Passing my name and my age. But how can I access this information? My first attempt is to call name or age method.

We can’t do it! We didn’t implement the name (and the age) method. Remember when I said that “In Ruby we use methods to direct access data”? To access the tk name and age we need to implement those methods on our Person class.

Now we can direct access this information. With encapsulation we can ensure that the object (tk in this case) is the only responsible to access the data (name and age). The internal representation of the object is hidden from the outside.

Inheritance: behaviors and characteristics

Certain objects have something in common. Behavior and characteristics.

For example, I inherited some characteristics and behaviors from my father. I inherited his eyes and hair as characteristics. And impatience and introversion as behavior.

In object oriented programming, classes can inherit common characteristics (data) and behavior (methods) from another class.

Let’s see another example and implement it in Ruby!

Imagine a car. Number of wheels, seating capacity and maximum velocity are all attributes of a car. We can say that an ElectricCar class inherits these same attributes from Car class.

Our Car class implemented! :)

Instantiated and we can use all methods created! Nice!

In Ruby, we use the < operator to a class inherits from another. An ElectricCar class can inherits from our Car class.

Simple as that! We don’t need to implement the initialize method and any other method, because this class already has it (inherited from Car class). Let’s prove it!

Beautiful!

Module: a toolbox

We can think of a module as a toolbox that contains a set of constants and methods.

An example of Ruby module is Math module. We can access the constant PI:

And .sqrt method:

And we can implement our own module and use it in classes.

We have a RunnerAthlete class.

And we implement a module Skill to have the average_speed method.

How do we add this module in our classes to have this behavior (average_speed method)? We just include it!

See the “include Skill”! And now we can use this method in our instance of RunnerAthlete class

Yay!

To finish modules, we need to understand that…

  • A module can have no instances.
  • A module can have no subclasses.
  • A module is defined by module ... end.

That’s it!

We learnt a lot of things about Ruby object oriented programming:

  • Encapsulation: hiding information
  • Inheritance: behaviors and characteristics
  • Module: a toolbox

I hope you guys can appreciate the content and learn more about how Ruby object oriented programming works. This is the forth part of Beautiful Ruby series.

If you didn’t see the part I, part II & part III, get the role series!

If you want a complete course, learn real-world coding skills and build projects, try One Month Ruby Bootcamp. See you there! ☺

Have fun, keep learning & always coding!

My Twitter & Github. ☺

--

--