Ruby III | Object-Orientation 💡

Welcome to the third instalment in my Ruby series…Object-orientation!

Ruby is an object-orientated programming language and the basis of OOP is that it allows us to organise our code, especially dealing with a large amount of complex code. The two main things about OOP are classes and of course objects. A class is a structure that represents a concept in the program and everything within ruby is pretty much an object.

To create a Class, we simply use the class keyword followed by the name we desire for it, for example, an Animal class would look like:

class Animal
attr_accessor :name, :species, :age #we will cover this later!
def initialize(name, species, age) #we will cover this later too!
@name = name
@species = species
@age = age
end
end

It is important to note that the class does nothing on its own but objects can be created from them which are individual products of the class.

When objects are created from a class they are always different from each other. This is important to always remember as this allows us to create many separate objects under the same umbrella. So, in the case of the Animal class, we can have many animals with different data (name, species, age, weight etc.) under the same class — the Animals 🦁🦒🐅🐬🦭.

So let’s create a new animal (object) within our Animal (class).

lion = Animal.new("Luthor", "Lion", 12)

The .new method allows us to create a new object and define its attributes in the parenthesis. These attributes were defined in the initialize method which we will discuss next.

The initialize method is executed when creating a new object along with its attributes.

In our example above, we are declaring the initialize method with a name, species and age as local variables. These local variables are then passed onto the instance variables @name, @species, and @age. The local variables hold the values that are passed along with the new method.

When creating the new object, we are passing in attributes and those values will be held by the local variables, as mentioned above.

In order to access attributes of an object we can simply do:

lion.name#"Luthor"

So, how does this work since instant variable data is private by default? Well, we can do it explicitly, which we have done by using an attribute accessor.

attr_accessor :name, :species, :age #we are now covering it yay!

What the attr_accessor does is, it joins both attr_reader and attr_writer. The attr_reader allows data to become publicly available and attr_writer allows you to change a value.

Class methods are class level methods which provide functionality to a class itself. We cannot directly call a class method on an instance and vice versa.

We define a class method by using the keyword self so now guess which method below is the initialize, the attribute accessor, the instance method and the class method.

class Animal
attr_accessor :name, :species, :age
def initialize(name, species, age)
@name = name
@species = species
@age = age
end
def self.wild_noises
puts "Animals go wild!"
end
def animal_noise
puts "Unique sound"
end
end

The attr_accessor is the attribute accessor, initialize is the intialize method (who would have guessed!? 😲), self.wild_noises is the class method and finally animal_noise is the instance method.

Inheritance allows us to share behaviours of a parent class to a subclass. This is done through a hierarchical structure where a given class can only ever directly be a subclass from one parent class.

class Animal
attr_accessor :name, :species, :age
def initialize(name, species, age)
@name = name
@species = species
@age = age
end
def self.wild_noises
puts "Animals go wild!"
end
def animal_noise
puts "Unique sound"
end
endclass Insect < Animal
def initialize(name, species, age)
super
end
end

In order to create a subclass, we would use < and then the parent class we are inheriting from.

Is it a bird? Is it a plane? No, it’s…super …just super!

This super keyword allows us to inherit from the parent class without explicitly stating the instance variable and local variable.

Now you know a bit more about OOP and the nature of a class, enjoy building more complex Ruby programs and explore the many possibilities with this new knowledge!

Software Engineer 👨🏽‍💻 | Full Stack Web Development 💻 | Smartphone Tech Enthusiast📱