Home » Ruby programming

Ruby constructors

Constructors in Ruby: Here, we are going to learn about the constructors in Ruby programming language with its examples.
Submitted by Hrithik Chandra Prasad, on August 16, 2019

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. 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.

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

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:

    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

Remember that –  a constructor is a special method of a class. It does not require an explicit call. In Ruby, constructors are invoked with the help of "new" keyword and a new keyword is used for creating an object of the class. The "new" keywords give an internal call to the "initialize" method and if the new has got some arguments specified, it will be passed to the initialize method. If the constructor is not parametric and “new” has some arguments then the compiler will generate an error known as "initialize: wrong number of arguments".

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.

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.