Home »
Ruby Tutorial
Classes in Ruby
By IncludeHelp Last updated : December 01, 2024
Ruby Classes
In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object or instance of the class 'laptop'. Thus, we can conclude that "A class is a blueprint or prototype that includes some methods and data members which are common to objects which are identical to each other in some way".
Ruby as a Pure Object-Oriented Language
Ruby is purely Object Oriented which means that its code may contain several classes and their instances. In Ruby, the concept of Reusability is achieved through the class as it allows the programmer to use the same prototype again and again to create objects which are similar to each other.
Class Hierarchy
Class hierarchy in Ruby is demonstrated through the following diagram:
Ruby provides you many classes. The above diagram does not include all of them. Class BasicObject is the superclass of all the classes in Ruby.
Creating Class in Ruby
Creating class in Ruby is not a very difficult task in Ruby. Its definition starts with the keyword "class" and closes with the keyword 'end'. Its basic syntax is as follows:
class (class _name)
end
Example:
class Student
end
The class "Student" is having no data member as well as a member function. We can create a class with data member and methods in the following manner:
class (class_name)
def (method_name)
end
end
We can create its object with the help of new keyword as shown below:
(instance_name) = (classname).new(parameters)
We can call its methods by using . operator like,
(instance_name).(method_name)
Example
class Student
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
Enter the number of students
36
The updated numbers of students are 36
In the above example, we have created a class Student. A method update is defined inside it with a local variable no_of_students.
In the main(), we have created an object or instance of the class Student and named it as record1. By using . operator with the instance name, we are accessing the method update.
Class Visibility/Access Control
The following are the three levels of method access control in Ruby:
- Private
- Public
- Protected
1. Private
Private methods can only be invoked in the context of the current object. You cannot call the private method directly in the main() method, If you are doing so, you will get an error as the visibility of private method is limited to the class in which it has been created.
For making a method Private, you need to use the keyword "private" before defining the method.
Syntax
private
def (method_name)
end
Example
class Student
private
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
// cannot use private members...
`<main>': private method `update' called for
#<Student:0x000000018998a0> (NoMethodError)
2. Public
by default, every method is public in Ruby i.e. they are free to be used by anyone – no access control is applied to them. In any case, if we explicitly want to make a method public, then we have to use the keyword 'public' along with the name of the method.
Syntax:
public
def (method_name)
end
Example
class Student
public
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
Enter the number of students
36
The updated numbers of students are 36
3. Protected
Protected methods are only accessible to the objects of defining class and its child class or subclass. They are mainly used during Inheritance (Parent-Child class Concept). The keyword "rotected" is used before the method name.
Syntax
protected
def (method_name)
end
Example
class Student
protected
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
`<main>': protected method `update' called for
#<Student:0x00000001e90948> (NoMethodError)