Object Oriented Programming and Ruby

Archit Joshi
3 min readMay 31, 2019

--

What is Object Oriented Programming ?

Object oriented programming is the way of looking the real world as a world full of objects with some definite shape, size and behavior. It is based on a model where we create objects and perform actions on them. This is done by sectioning off code to do small useful actions which we call “methods”.

Object

The word object is precisely a reference to a real word entity or at least a abstract representation of something that can be treated as an object. Be it ship or a simple thing like a bottle. Object in Object Oriented Programming has two basic properties, data and behavior.

Class

A class is the actual definition of an object or we can say a blueprint of an object. Class can be treated as a tool from which we can produce objects of a kind. For example once a blueprint of a car(the design of the car) is created, N number of identical cars can be created with different colors, or day different types of tires but the core is same.

We can demonstrate classes and objects with an easy ruby example :

class Car
def initialize(car)
@model_name = car[:model_name]
@car_class = car[:car_class]
@acceleration = car[:acceleration]
@top_speed = car[:top_speed]
@color = car[:color]
end
def change_car_color(new_color)
@color = new_color
end

def info
puts "The model #{@model_name} is a #{@car_class} class car with an acceleration of #{@acceleration} and #{@top_speed} top speed"
end
end

We can create an object of this class :

car_object = Car.new({model_name: "911 Turbo", 
car_class: "Sports",
acceleration: "0 to 60 is 5.2 seconds",
top_speed: "205 kmph",
color: "Yellow"
})
car_object.info

The line car_object.info invokes the method info on the newly created object and displays the result.

Note that we have used ruby’s standard terminology of declaring instance variables by using @, for example : @top_speed

We have two types of variables which we generally use in classes
1. Class variables.
2. Instance variables.
The issue with class variables is inheritance. So let us look at the concepts of Inheritance first to understand the underlying issue.

Inheritance

Suppose we want certain common behavior to be available to certain objects. For example a class Vehicle can have some common characteristics of all the vehicles such as number_of_wheels or seating_capacity, which class Car and a class Bike also needs so by inheriting these common properties from Vehicle class will allow us to reuse our code and later if a new class Bus has to be introduced then it can simply inherit vehicle and enjoy its properties.

Class variables vs Instance Variables

Now coming back to the difference between class and instance variables, lets us take an example.

class Vehicle
@@number_of_wheels = 4
def self.number_of_wheels
@@number_of_wheels
end
end

and another class Bike which inherits the class Vehicle

class Bike < Vehicle
@@number_of_wheels = 2
end

Now if we do

puts Bike.number_of_wheels        #==> 2
puts Vehicle.number_of_wheels #==> 2

It happened because when you set a class variable, you set it for the super class and all of the sub classes. The instance variables are for the instance of a class i.e. every object carries its own copy of instance variables defined it the class.

Hope you liked the basic introduction to the Object oriented programming with ruby. More features of the Object oriented programming such as Encapsulation, Polymorphism, Abstraction are yet to explored.

--

--

Archit Joshi
Archit Joshi

Written by Archit Joshi

Software developer. New to writing. Always up for a workout :)

No responses yet