Updated April 3, 2023
Introduction to Class in Ruby
Class in Ruby is an important attribute, Class are simply a clone for the object, any class can contains method, constant, etc and once we create the object from the class then that object contains all the properties of class, this the reason why we called it as the blueprint for any object, the main benefit of using class is it allow us to write methods and variables which can be reused by many objects, in Ruby we have class as the keyword to create any class with any name and new is another keyword to create object from that class and that object will have all the properties access.
How to Create Class in Ruby?
To create a class in Ruby we can use the class keyword before the name of the class. See the below example of the class creation in Ruby. Let me explain the below example.
- We have defined a class with class name ClassName, We can see that we have used class keyword before the class name(ClassName).
- 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 which does the initialization.
- When we create objects from the class at that time the class will also call initialize method automatically and do the work of initialization of variables.
Syntax:
class ClassName
#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
Object =ClassName.new(param1, param2)
How Does Class Works 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 class in Ruby plays the role of drawing and all the objects created from that class will play role of buildings. 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 A and this class contains a method and a constant.
- Here the class keyword will notify the Ruby compiler about the type of variable.
- Once Ruby compiler reads the class keyword it will reserve the memory and definition for a class with the name of class which we passed.
- Now we are creating many objects from the class A like object A1 , object A2 , object A3 and object A4.
- Each object created from this class contains the same method and constant inside it. Which means at any time if needed these objects can access the method a and variable x.
- In Ruby class there is a heap area which holds the data in the page format where each page has certain fixed sizes. As in the common languages like Java they used to have a heap area and a stack area, but in Ruby it has only heap area.In class we can read more about the heap area in Ruby documentations.
See the below flowchart of Ruby class for better understanding.
Examples
Below are the examples of Class in Ruby:
Example #1
Below is an example for the class creation and it’s uses. Aim of this program is to display the user information. We can explain the below example in the following steps.
- First, we have created a class with the name Human.
- Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize.
- We wrote a method display_data inside the class which will display the data for the attributes passed to it.
- Next, we are creating an object from the class Human and calling method display_data with the required parameters to display the user data.
Please see the below example of code along with a screen of output.
Code:
class Human
# Method for initialisation inside the class
def initialize()
# Initialising the variables
puts "Initialisation for the class will be done here"
end
def display_data(name,age,sex)
puts "The name of the user is #{name} and age is #{age} also he is a #{sex}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new()
object2 = Human.new()
object1.display_data("ranjan", 31, "male")
object2.display_data("anjali", 28, "female")
Output:
Example #2
Below is an example for the class creation and it’s uses. Aim of this program is to display the user information. We can explain the below example in the following steps.
- First, we have created a class with the name Human.
- 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_data 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 Human and calling method display_data 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 Human
# Method for initialisation inside the class
def initialize(name ,age ,sex )
# Initialising the variables
@name = name
@age = age
@sex =sex
end
def display_data()
puts "The name of the user is #{@name} and age is #{@age} also he is a #{@sex}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new("ranjan", 31, "male")
object2 = Human.new("anjali", 28, "female")
object1.display_data()
object2.display_data()
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of class in Ruby with the help of one useful and real example, we also learned the working of class with the help of diagram and little syntax , here we have given little focus on the uses of class in real world use cases also.
Recommended Article
We hope that this EDUCBA information on “Class in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.