Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Class & Object

  • Difficulty Level : Basic
  • Last Updated : 29 Jul, 2021

Ruby is an ideal object-oriented programming language. The features of an object-oriented programming language include data encapsulation, polymorphism, inheritance, data abstraction, operator overloading etc. In object-oriented programming classes and objects plays an important role. 
A class is a blueprint from which objects are created. The object is also called as an instance of a class. For Example, the animal is a class and mammals, birds, fish, reptiles, and amphibians are the instances of the class. Similarly, the sales department is the class and the objects of the class are sales data, sales manager, and secretary.
Defining a class in Ruby: 
In Ruby, one can easily create classes and objects. Simply write class keyword followed by the name of the class. The first letter of the class name should be in capital letter.

class Class_name


A class is terminated by end keyword and all the data members are lies in between class definition and end keyword.

# class name is Animal
class Animal

# class variables
@@type_of_animal = 4
@@no_of_animal = 3


Creating Objects using the “new” method in Ruby: 
Classes and objects are the most important part of Ruby. Like class objects are also easy to create, we can create a number of objects from a single class. In Ruby, objects are created by the new method.

object_name =


# class name is box
class Box

# class variable
@@No_of_color = 3


# Two Objects of Box class
sbox =
nbox =

Here Box is the name of the class and No_of_color is the variable of the class. sbox and nbox are the two objects of box class. You use (=) followed by the class name, dot operator, and new method.
Defining Method in Ruby: 
In Ruby member functions are called as methods. Every method is defined by the def keyword followed by a method name. The name of the method is always in lowercase and the method ends with end keyword. In Ruby, each class and methods end with end keyword.

def method_name

# statements or code to be executed




# Ruby program to illustrate
# the defining of methods
# defining class Vehicle
class GFG
# defining method
def geeks
# printing result
puts "Hello Geeks!"
# end of method
# end of class GFG
# creating object
obj =
# calling method using object


Hello Geeks!

Passing Parameters to new Method: 
User can pass any numbers of parameters to “new method” which are used to initialize the class variables. While passing parameters to “new method” it is must to declare an initialize method at the time of class creation. The initialize method is a specific method, which executes when the new method is called with parameters.


# Ruby program to illustrate the passing
# parameters to new method
# defining class Vehicle
class Vehicle
# initialize method
def initialize(id, color, name)
# variables
@veh_id = id
@veh_color = color
@veh_name = name
# displaying values
puts "ID is: #@veh_id"
puts "Color is: #@veh_color"
puts "Name is: #@veh_name"
puts "\n"
# Creating objects and passing parameters
# to new method
xveh = Vehicle. new("1", "Red", "ABC")
yveh = Vehicle. new("2", "Black", "XYZ")


ID is: 1
Color is: Red
Name is: ABC

ID is: 2
Color is: Black
Name is: XYZ

Explanation: Here Vehicle is the class name. def is a keyword which is used to define “initialize” method in Ruby. It is called whenever a new object is created. Whenever new class method called it always call initialize instance method. initialize method is like a constructor, whenever new objects are created initialize method called. Id, color, name, are the parameters in initialize method and @veh_id @veh_color, @veh_name are the local variables in initialize method with the help of these local variables we passed the value along the new method. The parameters in “new” method is always enclosed in double quotes.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!