Ruby | Access Control

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 w3wiki
  
     # public method without using 
     # public keyword    
     def Beginner_1
         puts "public method Beginner_1 is called"
     end
  
    # using public keyword
    public
     
     def Beginner_2
         puts "public method Beginner_2 is called"  
     end
       
     def Beginner_3
           
         puts "public method Beginner_3 is called"
           
         # calling Beginner_1 method
         Beginner_1
           
         # calling Beginner_1 method using
         # self-keyword
         self.Beginner_1
     end          
end
  
# creating the object of 
# class w3wiki
obj = w3wiki.new
  
# calling method Beginner_1
obj.Beginner_1
  
# calling method Beginner_2
obj.Beginner_2
  
# calling method Beginner3
obj.Beginner_3


Output:

public method Beginner_1 is called
public method Beginner_2 is called
public method Beginner_3 is called
public method Beginner_1 is called
public method Beginner_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 w3wiki
       
    # using initialize method
    # it can't be private
    def initialize
        puts "This is the initialize Method"
     end
      
    # public method
    def Beginner_1
          
             puts "Public Beginner_1 Method"
         end
      
    # using the private keyword to 
    # declare a private method
    private
      
     def Beginner_2
           
         puts "This is Private Method"  
     end 
       
end          
  
# creating the object of 
# the class w3wiki
obj = w3wiki.new
  
# calling method Beginner_1
# (Beginner1 method is public method)
obj.Beginner_1
  
# calling private method will give the error
obj.Beginner_2


Error:

source_file.rb:41:in `

‘: private method `Beginner_2’ called for # (NoMethodError)
Did you mean? Beginner_1

This is the initialize Method
Public Beginner_1 Method

Protected Method

Protected methods can only be called by objects of the defined class and its subclass. The access of these methods is limited in between the defined class or its subclass. You cannot access protected methods outside the defined class or its subclass. The usage of protected methods is finite. Protected methods are defined using protected keyword.

Example:




# Ruby program to demonstrate 
# the protected access control
  
#!/usr/bin/ruby
  
class w3wiki
    
    # using initialize method
    def initialize
          
        # calling Beginner_2 method
        Beginner_2
          
        # calling Beginner_2 method 
        # using self-keyword
        self.Beginner_2
          
     end
      
    # public method
    def Beginner_1
         puts " Beginner_1 method is called"  
     end 
      
    # defining the protected method using
    # protected keyword
    protected
      
    def Beginner_2
         puts " Beginner_2 method is called"  
    end
  
end
  
# creating the object of class w3wiki
obj = w3wiki.new
  
# calling method
obj.Beginner_1
  
  
# if you will try to call protected method 
# using the object of class then it will 
# give error
obj.Beginner_2


Error:

source_file.rb:45:in `

‘: protected method `Beginner_2’ called for # (NoMethodError)
Did you mean? Beginner_1

Beginner_2 method is called
Beginner_2 method is called
Beginner_1 method is called


Note:

  • The user can call the private and protected methods inside a public method of the same class.

    Example:




    # Ruby program to demonstrate the calling 
    # of private and protected method in the 
    # public method
      
    class Beginner
       
        # public method
        def method_1
        
         p "Public Method of class Beginner"
           
            # calling protected and private method
            # inside the public method
            method_2
            method_3
        end
       
        # defining the protected method
        protected
       
        def method_2
              
            p "Protected Method of class Beginner"
         end
       
        # defining the private method   
        private
       
        def method_3
              
           p "Private Method of class Beginner"
              
         end
    end
       
    # creating an object of class Beginner
    obj = Beginner.new
      
    # calling the public method of class Beginner
    obj.method_1

    
    

    Output:

    "Public Method of class Beginner"
    "Protected Method of class Beginner"
    "Private Method of class Beginner"
    
  • In general, private methods can’t be inherited in object-oriented programming languages. But in Ruby, private methods can also be inherited just like protected and public methods.

    Example:




    # Ruby program to demonstrate that private  
    # method can also be inherited
      
    class Beginner
       
        # public method
        def method_1
        
            p "Public Method of class Beginner"
         
        end
       
        # defining the protected method
        protected
       
        def method_2
              
            p "Protected Method of class Beginner"
         end
       
        # defining the private method   
        private
       
        def method_3
              
            p "Private Method of class Beginner"
              
         end
    end
      
    # Sudo class inheriting Beginner class
    class Sudo < Beginner
         
        # public method of Sudo class
        def method_4
              
            p "Public Method of Sudo Class"
              
            # calling all three methods 
            # of Beginner class
            method_1
            method_2
            method_3
        end
    end
       
    # creating an object of class Sudo
    obj_sudo = Sudo.new
      
    # calling the public method
    # of class Sudo which will
    # automatically call the private 
    # and protected method of Beginner class
    obj_sudo.method_4

    
    

    Output:

    "Public Method of Sudo Class"
    "Public Method of class Beginner"
    "Protected Method of class Beginner"
    "Private Method of class Beginner"
    
  • The public method can be accessed outside the class in which they are defined. But user can’t access the private and protected methods outside the class in which they were defined.

    Example:




    # Ruby program to demonstrate that private  
    # and protected method can't be accessed 
    # outside the class even after inheritance
      
    class Beginner
       
        # public method
        def method_1
        
            p "Public Method of class Beginner"
         
        end
       
        # defining the protected method
        protected
       
        def method_2
              
            p "Protected Method of class Beginner"
         end
       
        # defining the private method   
        private
       
        def method_3
              
            p "Private Method of class Beginner"
              
         end
    end
      
    # Sudo class inheriting Beginner class
    class Sudo < Beginner
         
        # public method of Sudo class
        def method_4
              
            p "Public Method of Sudo Class"
              
        end
    end
       
    # creating an object of class Sudo
    obj_sudo = Sudo.new
      
    # calling the public method
    # of class Sudo and Beginner
    obj_sudo.method_4
    obj_sudo.method_1
      
    # if you will try to call the protected
    # and private method using the object 
    # of class Sudo, then it will give error
    obj_sudo.method_2
    obj_sudo.method_3

    
    

    Error:

    source_file.rb:54:in `

    ‘: protected method `method_2’ called for # (NoMethodError)
    Did you mean? method
    method_1
    method_4
    methods

    “Public Method of Sudo Class”
    “Public Method of class Beginner”

  • The main difference between the protected and private methods is that protected methods are accessible from inside the class by using an explicit receiver while private methods are not.

    Example:




    # Ruby program to demonstrate that private  
    # and protected method can't be accessed 
    # outside the class even after inheritance
      
    class Beginner
       
        # public method
        def method_1
        
            p "Public Method of class Beginner"
         
        end
       
        # defining the protected method
        protected
       
        def method_2
              
            p "Protected Method of class Beginner"
         end
       
        # defining the private method   
        private
       
        def method_3
              
            p "Private Method of class Beginner"
              
         end
    end
      
    # Sudo class inheriting Beginner class
    class Sudo < Beginner
         
        # public method of Sudo class
        def method_4
              
            p "Public Method of Sudo Class"
              
          
          
            # calling the public method 
            # of Beginner class
            method_1
      
            # creating object of class Sudo
            # inside the public method of 
            # class Sudo
            obj_inside_sudo = Sudo.new
      
            # calling the protected  
            # method of class Beginner
            obj_inside_sudo.method_2
              
            # calling the private  
            # method of class Beginner
            # using an explicit receiver
            obj_inside_sudo.method_3 rescue p "You can't Access!" 
           
      
              
        end
    end
       
      
      
    # creating an object of class Sudo
    obj_sudo = Sudo.new
      
    # calling the public method
    # of class Sudo 
    obj_sudo.method_4

    
    

    Output:

    "Public Method of Sudo Class"
    "Public Method of class Beginner"
    "Protected Method of class Beginner"
    "You can't Access!"
    
  • To define multiple protected and private methods in a single class, you can use the following syntax:
    class class_name
    
      # this method is public 
      def public_method        
      end  
    
      public :method_1
      
      protected :method_2, :method_3
      
      private :method_4, :method_5  
    
    end