Ruby: What is an object?
Code Newbie? You’re in the right place! Let’s learn about objects.
So, we know that Ruby is an object oriented programming (OOP) language and that everything in Ruby is an object. But, what does that EVEN mean I hear you say?
What is an object?
Before we dive straight into objects, let’s introduce the term, class.
Classes define objects. For example, let’s create a class called “Rapper”. The objects for our class could be, “Nicki Minaj” and “Cardi B”. They are now both instances of the “Rapper” class.
Of course Nicki Minaj and Cardi B are completely different people. Likewise, objects are not all alike. They are differentiated by their properties and their functions. A property of the Rapper class could be “songs” that the rappers have produced and we could use this property to name the songs of each rapper. These are called instance variables in Ruby. We could also define methods for the Rapper class, these are actions that rappers can take.
Why is OOP cool?
Overall, object oriented programming makes our lives waaay easier as programmers. Let’s take an example.
So back in the day, a procedural style of programming (such as that used in the language C) was used, which means you focused on the steps required to complete a task, not how data was managed.
As applications grew they became difficult to maintain. We needed a way to change part of the codebase, which wouldn’t affect the whole programme.
Welcome to the stage, OOP.
What is OOP?
Object oriented programming is a way of organising a programming language. Ruby isn’t the only language that uses OOP, there are others such as Python and Java, which do too.
OOP has four key concepts, let’s look at them:
Encapsulation: I know this word sounds scary, but fret not dear friend, it’s an easy concept! It just means that certain parts of our code do certain things and other parts of our code can’t access these functionalities.
An example is going out clubbing. Let’s say we go to a club with different rooms; one for Drum and Bass, one for Hip Hop and another for Reggae. The Reggae room will not suddenly play Hip Hop, each room will maintain their state.
Likewise, Ruby creates objects. Their states are kept private in their class.
Another great thing is that these objects, even if we utilise them in different places in our codebase, they will remain the same. Let’s say we have some old, dusty albums lying around. Our Akon album, no matter which CD player we put it into, will always be Akon.
This is really useful for us as programmers, because it means we can reuse the same code and reduce the complexity of our codebase.
Abstraction is an amazing concept, which we literally cannot function without in our everyday lives. It means that we only show the essential and we hide unnecessary complexities.
An example of this, is having a baby. As women we create humans, without even understanding the intricate biological processes taking place within us. We know what we need to, when we need to. Bring on those contractions mama, coz I need to come out! That’s abstraction, baby.
Another example is talking about where you come from. Let’s say you come from Abeokuta in Ogun state. Most people don’t know where that is, so we can perform an abstraction. We can say, I am Nigerian. We can perform yet another abstraction and say, I am African.
Ruby achieves abstraction by hiding functionalities available to objects unless relevant. We’ll learn more about these functionalities, called methods, later.
Inheritance is used in Ruby, again to make our lives easier. Often objects can be very similar, but they may have a few unique things that differentiate them.
Inheritance means that we can create a parent and child relationship between our classes. Our child inherits all of the functionalities of the parent, but can have it’s own unique functionalities too.
The parent is referred to as the superclass. This means that we can create classes that we use time and time again and then we just use smaller subclasses to achieve more detailed behaviours.
This means we don’t have to redefine the same properties and methods for every object and repeat a gazillion lines of code. Fantastic!
Polymorphic structures allow our objects to have different abilities that are appropriate to them.
I know this is a bit mathsy, but I really love this example; let’s say we make a class, shape. Then we create a method which defines the area of any objects in this class, so even if the objects are a triangle, rectangle or circle we will get the correct area computation. Magic, right.
As an addition to mentioning methods, Ruby also offers us modules. These are similar to classes in that they share behaviours, but you can’t create an object with a module.
A module must be mixed in with a class. This is called a mixin. After mixing in a module, the behaviours declared in that module are available to the class and its objects.
References
http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html
https://launchschool.com/books/oo_ruby/read/the_object_model#whyobjectorientedprogramming
https://www.eriktrautman.com/posts/ruby-explained-objects-and-methods
https://medium.freecodecamp.org/object-oriented-programming-concepts-21bb035f7260
https://www.upwork.com/hiring/development/object-oriented-programming/
https://www.webopedia.com/TERM/P/polymorphism.html
Shout out to Mosh Hamedani, who I absolutely ADORE. He’s got a lovely youtube vid on OOP here.