Updated May 16, 2023
Introduction to Ruby Constructor
A constructor in Ruby is part of a class attribute, and it is a predefined method that depends on us we want to use a constructor or not; the primary purpose of a constructor is to initialize the essential variables and required things before making any method calls of the class, the good thing about the constructor is we do not need to call constructor function for initialization of the class it will be initialized on the object creation from the class, to use the constructor method in any class of Ruby we can use initialize method. It will get called on object creation from the class.
Syntax
Below is the syntax for the constructor where we are creating a constructor by using the new operator; once we use the new operator for creating the Object, it will automatically call the initialize method, where we can do some initialization work for other methods of this class. To create an Object in Ruby, we can use the new keyword with the name of the class, like ConstructorExample.new, and once we use this command, it will internally call the method to initialize. See the below example for the use of a constructor in Ruby.
Let me explain the below example:
- We have defined a class with the class name ConstructorExample; We can see that we have used the class keyword before the class name(ConstructorExample). We will use this class name to create objects using a new keyword and do not need to call the constructor manually here.
- Inside the constructor method, we have given some comment sections; here, instead of comments, we can also put some methods and constants as the initialization.
- Finally, we create an object from the class ConstructorExample with the help of a new keyword which makes the call to initialize the method automatically.
- When we create objects from the class at that time, the class will also initialize the method automatically and do the work of initializing variables. Here Object contains all the variables and methods of the ConstructorExample class.
Please see the syntax below for the class creation in Ruby.
class ConstructorExample
#Do the initialisation here
def initialize(param1, param2)
# Here we are initialising the variables .
#The initialisation can be setting value of param1 and param2 for uses in other method of this class
end
#Write here your methods and constants
end
#Here we are creating objects from class and at the same time it will call the initialize method where we can do some initialisation .
constructorInitialize1 =ConstructorExample.new(param1, param2)#Here automatic call for constructor will happen
constructorInitialize1 =ConstructorExample.new(param1, param2)#Here automatic call for constructor will happen
constructorInitialize1 =ConstructorExample.new(param1, param2)#Here automatic call for constructor will happen
constructorInitialize1 =ConstructorExample.new(param1, param2)#Here automatic call for constructor will happen
How does Constructor work in Ruby?
The constructor is one of the most important components of any class; it always depends on our requirement for the use of a a constructor, suppose we want to make some initialization so that next time any call of a method will be there from the class, then that method will use some fixed data for any call, in that case, we can set some initial variables and every method which will get call will use the data set at the time of initialization by the constructor.
We can explain the constructor with the help of the below diagram.
- Here we have one class with the name ABC, which contains a method and a constant and initialize method,, which will get called by default without making manual calls at the time of object creation.
- Here the new keyword will notify the Ruby compiler about instructions for creating an object from the class name ABC, and it will lead to checking if any initialize method and any code inside is there. If there, then execute them first.
- Once the Ruby compiler reads the new keyword, it will create an object for the class taking all properties and initializing the constructor call.
- Now we are creating many objects from the class ABC, like object A1, object A2, object A3, and object A4,, and with each object creation, there will be a constructor initialize call for initialization of constants and variables.
See the below flowchart of the Ruby constructor for a better understanding.
Examples of Ruby Constructor
Here are the following examples of ruby Constructors:
Example #1
Here we are not initializing anything inside the initialize method. We have only printed a few messages to show you how to initialize the method that gets called without calling on object creation from the class.
- First, we have created a class with the name Car.
- Next, we have created a Ruby inbuilt method initialize, which will be used as the constructor inside the Ruby class to initialize basic things.
- We wrote a method display_car inside the class, displaying the data for the attributes passed to it.
- Next, we create an object from the class Car with the help of a new keyword and calling method display_car with the required parameters to display the car details.
Please see the below example of code, along with an output screen.
Code:
class Car
# Method for initialisation inside the class
def initialize()
# Initialising work
puts "Initialisation for the class Car will be done here"
end
def display_car(name,model,price)
puts "The Car is #{name} and model number is #{model} price of car is #{price}"
end
end
# Creating an objects and passing parameters for initialization to constructor
carObjec1 = Car.new()
carObjec2 = Car.new()
carObjec1.display_car("Tata", 'Tata123', 500000)
carObjec2.display_car("Tata", 'Tata124', 520000)
Output:
Example #2
Below is an example of using a constructor initialized with a class Car. This program aims to display Car information. We can explain the below example in the following steps.
- First, we have created a class with the name Car. With the help of the Car class, we will create objects using a new keyword, and once we create the object of the Car class, it calls initialize method and sets the basic car details so that the next time any call happens, the initial set value will be shown.
- We wrote a method display_car inside the class, which will display the data for the attributes passed to the initialize method(playing the role of the constructor)at the time of object creation.
Please see the below example of code, along with an output screen.
Code:
class Car
# Method for initialisation inside the class
def initialize(name ,model ,price )
# Initialising
@name = name
@model = model
@price =price
end
def display_car()
puts "The car is #{@name} and model number is #{@model} and the price of car is #{@price}"
end
end
# Creating an objects and passing parameters for initialization as the constructor will get automatically call
carObjec1 = Car.new("Tata", 'Tata123', 500000)
carObjec1 = Car.new("Tata", 'Tata124', 520000)
carObjec1.display_car()
carObjec1.display_car()
Output:
Conclusion
These tutorials taught us some important uses and basic concepts of constructors in Ruby with the help of one useful and real example. We also learned the working of the constructor with the help of a diagram and little syntax; here, we have also given little focus on the uses of the constructor in real-world use cases.
Recommended Articles
We hope that this EDUCBA information on “Ruby Constructor” was beneficial to you. You can view EDUCBA’s recommended articles for more information.