Introduction to Ruby Constructor
Constructor in Ruby is part of class attribute and it is a predefined method which depends on us we want to use constructor or not, the main purpose of a constructor is to initialize the basic variables and required things before making any method calls of the class, the good thing about the constructor is we do not require 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, and 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 use of 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 use of constructor in Ruby.
Let me explain the below example:
- We have defined a class with class name ConstructorExample, We can see that we have used class keyword before the class name(ConstructorExample). We will use this class name to create objects using a new keyword and we do not require to call constructor manually here.
- Inside the constructor method, we have given some comment sections, here in place of comments we can also put some methods and constants as the initialization.
- Finally, we are creating 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 initialization of 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 use of constructor, suppose we want to make some initialization so that from next time any call of 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 constructor with the help of the below diagram.
- Here we have one class with the name ABC and this class 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 instruction for the creation of an object from class name ABC and it will lead to checking if any initialize method and any code inside is there or not if there then execute them at first.
- Once Ruby compiler reads the new keyword it will create an object for the class along with 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 by each object creation there will be a constructor initialize call for initialization of constants and variables.
See the below flowchart of Ruby constructor for better understanding.
Examples of Ruby Constructor
Here are the following examples of ruby Constructor:
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 method 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 which will display the data for the attributes passed to it.
- Next, we are creating 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 a screen of output.
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 where we are using constructor initialize 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 as with help of Car class we will create objects using a new keyword and once create the object of the Car class it calls initialize method and set the basic car details so that next time when 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 a screen of output.
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
From these tutorials, we learned some important uses and basic concepts of constructor 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 given little focus on the uses of constructor in real-world use cases also.
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.
600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6
View Course
Related Courses