Home »
Ruby Tutorial
Ruby constructors
By IncludeHelp Last updated : December 01, 2024
Introduction to Constructors in Ruby
Constructors are one of the most important parts of Object-Oriented programming. A constructor of a class is a unique method which is invoked or called when an object of that particular class is being instantiated. It does not require an explicit statement for their calling.
Purpose of Constructors
The main purpose of constructors is to facilitate initialization to the variables of the class during the process of object creation. Constructors work same as a normal method, they do contain statements, expressions, and method calls.
Constructors in Ruby vs Other Languages
In other object-oriented languages like C++ and Java, the name of class and constructor is used to be the same but in the case of Ruby, the name is different from the name of the class. In Ruby, you can define a constructor with the help of the following syntax,
def initialize
#expressions
end
Syntax for Defining a Constructor
A constructor returns the object of that class. It is not possible to provide an inheritance to the constructor whereas it is permissible to overload a constructor. The constructors are defined within a class in the following way:
Constructor without arguments
class CLASS_NAME
def initialize
# expressions
end
# other methods
end
The above is a syntax of a constructor with no arguments. Go through the following syntax to understand constructor with arguments,
Constructor with arguments
class CLASS_NAME
def initialize(argument list)
# expressions
end
# other methods
end
Key Characteristics of Constructors
- Constructors are invoked using the
new
keyword.
- The
new
keyword internally calls the initialize
method.
- Providing arguments to
new
passes them to the initialize
method.
- Errors occur if arguments provided to
new
do not match the constructor's parameter list.
Example: Non-Parametric Constructor
Let us understand the example of a non-parametric constructor
=begin
Ruby program to demonstrate constructor
without parameters
=end
class ClassA
#non parametric constructor
def initialize
for i in 0..10
puts "Welcome to Includehelp.com"
end
end
end
#class instantiation
ClassA.new
Output
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
Welcome to Includehelp.com
You can observe in the above code that when we are giving an explicit call to "new" keyword, it is in return giving an internal call to initialize method.
Example: Parametric Constructor
Now, understand parametric constructor with the help of example given below,
=begin
Ruby program to demonstrate
constructor with parameters
=end
class ClassA
#parametric constructor
def initialize(a,b)
@instA=a
@instB=b
end
def prnt
puts "First value is : #{@instA}"
puts "Second value is : #{@instB}"
end
end
#class instantiations
ob1=ClassA.new(10,20)
ob2=ClassA.new(13,200)
ob1.prnt
ob2.prnt
Output
First value is : 10
Second value is : 20
First value is : 13
Second value is : 200
In the above example, you can observe that we are providing values to "new" and "new" is proving those values to initialize method.