×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to create the multiple objects of the class

Last Updated : December 15, 2025

Problem Solution

In this program, we will create a class Student with 3 data members id, name, and fee. Here, we will create methods to initialize and print data members. Then we will create 3 objects of the Student class and set and print student information.

Program/Source Code

The source code to create the multiple objects of the class is given below. The given program is compiled and executed successfully.

# Ruby program to create the 
# multiple objects of the class

class Student
    
    def initialize(id, name, fee)
        @id   = id;
        @name = name;
        @fee  = fee;    
    end
    
    def PrintStudentInfo()
        print "Student Id:   ",@id,"\n";
        print "Student Name: ",@name,"\n";
        print "Student Fee:  ",@fee,"\n\n";
    end
        
end

obj1 = Student.new(101,"Rahul", 5000);
obj1.PrintStudentInfo();

obj2 = Student.new(102,"Rohit", 7000);
obj2.PrintStudentInfo();

obj3 = Student.new(103,"Virat", 9000);
obj3.PrintStudentInfo();

Output

Student Id:   101
Student Name: Rahul
Student Fee:  5000

Student Id:   102
Student Name: Rohit
Student Fee:  7000

Student Id:   103
Student Name: Virat
Student Fee:  9000

Explanation

In the above program, we created a class called Student. The Student class contains data member's id, name, and fee. Then we defined the initialize() method to initialize the data members and we also created a PrintStudentInfo() method to print student information. Then we created 3 objects of the class and initialized the data members and called PrintStudentInfo() method to print student information.

Ruby Classes & Objects Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.