Ruby | Class & Object

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.Syntax: 
class Class_name

end
A class is terminated by end keyword and all the data members are lies in between class definition and end keyword.Example:  
# class name is Animal
class Animal

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

end
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.Syntax: 
object_name = Class_name.new
Example:  
# class name is box
class Box

# class variable
@@No_of_color = 3

end

# Two Objects of Box class
sbox = Box.new
nbox = Box.new
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.Syntax:  
def method_name

# statements or code to be executed

end
Example: 
Ruby
# Ruby program to illustrate# the defining of methods #!/usr/bin/ruby # defining class Vehicleclass GFG # defining methoddef geeks # printing resultputs "Hello Geeks!" # end of methodend # end of class GFGend # creating objectobj = GFG.new # calling method using objectobj.geeks
Output:  
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.Example: 
Ruby
# Ruby program to illustrate the passing# parameters to new method #!/usr/bin/ruby # defining class Vehicleclass Vehicle # initialize methoddef initialize(id, color, name) # variables@veh_id = id@veh_color = color@veh_name = name # displaying valuesputs "ID is: #@veh_id"puts "Color is: #@veh_color"puts "Name is: #@veh_name"puts "\n"endend # Creating objects and passing parameters# to new methodxveh = Vehicle. new("1", "Red", "ABC")yveh = Vehicle. new("2", "Black", "XYZ")
Output:  
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. 
ghanshyam anand
Ruby-Basics
Ruby-OOP
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Make a Custom Array of Hashes in Ruby?
Global Variable in Ruby
Ruby | Enumerator each_with_index function
Ruby | Case Statement
Ruby | Array select() function
Ruby | Hash delete() function
Ruby | unless Statement and unless Modifier
Ruby | Data Types
Ruby | String capitalize() Method
Ruby | String gsub! Method