Updated April 5, 2023
Definition of Ruby Objects
Ruby is a purely object-oriented language, everything in Ruby is an object because Ruby supports everything like encapsulation, inheritance, operator overloading, and polymorphism, etc, to create an object in Ruby we can use the new keyword, we can create an unlimited object from any class and each object can access the attributes of the class (like a method of class and constantly defined inside the class), we can access an attribute by object using object created by class name, most important uses of the object is they are very portable as they can contain any data like method and variable (obj.methodname and object.variableName) also each object points to the same memory location for accessing method and variable without allocating new memory.
How to Create Objects in Ruby?
To create an Object in Ruby we can use the new keyword with the name of the class like ABC.new.See the below example of the Object creation in Ruby. Let me explain the below example.
- We have defined a class with class name ABC, We can see that we have used class keyword before the class name(ABC). We will use this class name to create objects using a new keyword.
- Inside the class, we have given some comment sections, here in place of comments we can also put some methods and constants.
- Inside the class we have used a method initialize method, it is a predefined method that does the initialization.
- Finally, we are creating an object from the class ClassName with the help of a new
- 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 ABC class.
Please see the syntax below for the class creation in Ruby.
class ABC
#Do the initialisation here
def initialize(param1, param2)
# Here we are initialising the variables .
@param1 = param1
@param2 = param2
end
#Write here your methods and constants
end
#Create object from class ClassName
A1 =ABC.new(param1, param2)#Creation Of Object A1
A2 =ABC.new(param1, param2)#Creation Of Object A2
A3 =ABC.new(param1, param2)#Creation Of Object A3
A4 =ABC.new(param1, param2)#Creation Of Object A4
How do Objects Work in Ruby?
If you have seen any civil engineer designing or drawing some architecture for the buildings for architecture for bridges. Once a civil engineer draws any design that design can be used to create many buildings. Because that drawing contains everything which we need to create any buildings or bridges. In the same way objects in Ruby play the role of buildings and all the classes play the role of paper of drawing. Every time we create an object and the object contains the properties of classes. We can explain the working of class in Ruby can be explained with the help of the flowchart below.
- Here we have one class with the name ABC and this class contains a method and a constant.
- Here the new keyword will notify the Ruby compiler about instruction for the creation of an object from class name ABC.
- Once Ruby compiler reads the new keyword it will give access or more technically say associate the memory of the methods and variables of the class attributes.
- Now we are creating many objects from the class ABC like object A1, object A2, object A3, and object A4.
- Each object created from this class contains the same method and constant inside it. This means at any time if needed these objects can access the method M and variable C.
See the below flowchart of Ruby’s class for better understanding.
Examples
Below is an example for the object creation and it’s uses in Ruby. The aim of this program is to display the personal information. We can explain the below example in the following steps.
- First, we have created a class with the name Person.
- 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_person inside the class which will display the data for the attributes passed to it.
- Next, we are creating an object from the class Person with the help of a new keyword and calling method display_person with required parameters to display the person data.
Example #1
Please see the below example of code along with a screen of output.
Code:
class Person
# Method for initialisation inside the class
def initialize()
# Initialising work
puts "Initialisation for the class Person will be done here"
end
def display_person(name,age,sex)
puts "The name of the person is #{name} and age is #{age} also he is a #{sex}"
end
end
# Creating an objects and passing parameters for initialization
personObjec1 = Person.new()
personObject2 = Person.new()
personObjec1.display_person("Ranjan", 31, "male")
personObjec1.display_person("Manisha", 28, "female")
Output:
Example #2
Below is an example of the object creation and it’s uses in Ruby. The aim of this program is to display personal information. We can explain the below example in the following steps.
- First, we have created a class with the name Person as with help of Person class we will create objects using a new keyword.
- Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize user details for use in other methods.
- We wrote a method display_person inside the class which will display the data for the attributes passed to initialize at the time of object creation.
- Next, we are creating an object from the class Person and calling method display_person with required no parameters as the parameters will be used which we have passed at the time of initialization.
Please see the below example of code along with a screen of output.
Code:
class Person
# Method for initialisation inside the class
def initialize(name ,age ,sex )
# Initialising
@name = name
@age = age
@sex =sex
end
def display_person()
puts "The name of the person is #{@name} and age is #{@age} also he is a #{@sex}"
end
end
# Creating an objects and passing parameters for initialization
personObjec1 = Person.new("Ranjan", 31, "male")
personObjec2 = Person.new("Manisha", 28, "female")
personObjec1.display_person()
personObjec2.display_person()
Output:
Recommended Articles
This is a guide to Ruby Objects. Here we also discuss the definition and how to create objects in ruby along with different examples and its code implementation. You may also have a look at the following articles to learn more –