Home »
Ruby Tutorial
Object oriented programming in Ruby
By IncludeHelp Last updated : December 01, 2024
Before getting into understanding how Object-oriented programming is implemented in Ruby, let us first understand what Object Oriented means.
What is Object-oriented programming?
Object-oriented programming reduces the complexity of large software systems thus making the software easier to maintain. This category of programming basically uses Objects. In a pure object-oriented language, everything is considered as an Object. The main aim of OOP is to combine the data and functions which are operating those data so that the data cannot be accessed by any other part of the code.
Object-oriented programming in Ruby
Ruby is a pure Object oriented language and everything is considered as an object. Even Strings, Numbers, true or false is an Object being the most primitive type. Rest of the article will let you know, how classes and objects are implemented in Ruby?
Class
Class is nothing but a blueprint of a data type. It simply defines, what an instance of the class consists and possible functions which can be performed on the object of the class. In Ruby, even classes are objects of "class" class.
Syntax
class Class_name
#code
end
Remember that, the class name must start with a capital letter by convention. You will get an error at Compile time when the convention is not followed.
Now, let us declare a class in Ruby,
class Example
def initialize
end
def prints
end
end
The above code will be compiled but not yield an output because no memory is provided to the class until it is not instantiated.
Objects
Objects are the instance of a class. They provide memory to the class. You can create any number of objects of a class. The objects are created with the help of the "new" keyword.
Syntax
object_name = class_name.new
Example
Let us understand object creation with the help of an example:
class Example
def initialize
end
def prints
puts "Hello fella. How are you!!"
end
end
ob1 = Example.new
ob1.prints
ob2 = Example.new
ob2.prints
Output
Hello fella. How are you!!
Hello fella. How are you!!
In the above code, you can observe that we are creating two objects of class Example. Then we are invoking prints method with the instances.
Constructors
Constructors are used to initialize the variable of a class. They initialize class variables at the time of object creation. ‘initialize’ method works as a constructor in Ruby. It is defined inside the class and is invoked with the creation of an object. Go through the syntax and example for a better understanding.
Syntax
class Class_name
def initialize(parameters if required)
end
end
Example
=begin
Ruby program to demonstrate initialize.
=end
class Example
def initialize(j,k)
@a = k
@b = j
end
def prints
puts "The value of class variables are #{@a} and #{@b}"
end
end
ob1 = Example.new(2,5)
ob1.prints
ob2 = Example.new(9,7)
ob2.prints
Output
The value of class variables are 5 and 2
The value of class variables are 7 and 9