Access control is a very important part of the object-oriented programming language which is used to restrict the visibility of methods and member fields to protect data from the accidental modification. In terms of access control, Ruby is different from all other Object Oriented Programming languages.
Important Points about Ruby Access Control:
- The visibility of the class variable and instance is always private.
- Access controls are only applicable to methods.
- We can’t apply any access control to the instance and the class variables.
- The private methods in Ruby can also be inherited just like public and protected methods.
In Ruby, access control work on two conditions:
- First, from where the method is called, i.e inside or outside of the class definition.
- Second, the self-keyword is included or not. Basically, self-keyword is used to point to the current recipient.
In Ruby, it is not necessary for the inheritance to involve in access control, like in C++ access control are used in inheritance. Access control is of three types as shown in the below image:
Public Method
Public Methods are those methods which can be called by anyone. As we know that access control works only on methods, so by default all the methods are public. But still, we can define a public method explicitly using the public keyword. Public methods are generally called outside the class.
Example:
# Ruby program to demonstrate # the public access control #!/usr/bin/ruby # taking a class class GeeksforGeeks
# public method without using
# public keyword
def geeks_1
puts "public method geeks_1 is called"
end
# using public keyword
public
def geeks_2
puts "public method geeks_2 is called" end
def geeks_3
puts "public method geeks_3 is called"
# calling geeks_1 method
geeks_1
# calling geeks_1 method using
# self-keyword
self .geeks_1
end end # creating the object of # class GeeksforGeeks obj = GeeksforGeeks. new
# calling method geeks_1 obj.geeks_1 # calling method geeks_2 obj.geeks_2 # calling method geeks3 obj.geeks_3 |
Output:
public method geeks_1 is called public method geeks_2 is called public method geeks_3 is called public method geeks_1 is called public method geeks_1 is called
Private Method
Private methods are those methods which are not accessible outside the class or in other words, private methods are called only inside the class definition. The methods of the class can access private members. In private methods, we do not use the self-keyword. By default, initialize method will be private method. The user cannot make the initialize method as the public method. A private method is defined by using private keyword.
Note: As we know that private methods are strictly restricted for their visibility, only defined class members can access these methods, but they can be inherited by the subclass. A subclass can access them and can override them.
Example:
# Ruby program to demonstrate # the private access control #!/usr/bin/ruby # creating class class GeeksforGeeks
# using initialize method
# it can't be private
def initialize
puts "This is the initialize Method"
end
# public method
def geeks_1
puts "Public geeks_1 Method"
end
# using the private keyword to
# declare a private method
private
def geeks_2
puts "This is Private Method" end end # creating the object of # the class GeeksforGeeks obj = GeeksforGeeks. new
# calling method geeks_1 # (geeks1 method is public method) obj.geeks_1 # calling private method will give the error obj.geeks_2 |
Error:
source_file.rb:41:in `
‘: private method `geeks_2’ called for #
|
‘’
-
- ’
<
- ’
<
‘’ “”
“” -
<