Home » Ruby programming

Access Control in Ruby

Ruby | Access Control: In this tutorial, we are going to learn about the various access controls (private, public & protected) in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on April 09, 2020

Ruby | Access Control

Here, we will study Access control in Ruby which is an important topic for any Object-Oriented Language and we are well aware of the fact that Ruby is a pure object-oriented language. You must have got confused with the topic but we will remove all your confusion in the rest of the content with the help of illustrating program codes and syntaxes.

Access control is a way to hinder the visibility of methods and member variables to secure data from unwanted modifications but if we talk about Ruby, Ruby is slightly different from other pure object-oriented languages in terms of access control. It is different in the way that you can't apply any kind of access control to the instance and class variables as their visibility are always private. You can apply access controls to the methods.

There are three types of access control available in Ruby. They are mainly public, private and protected. In C++, you use access controls only while doing Inheritance but this is not the case of Ruby.

Let us discuss each access control in a detailed manner.

1) Private method

They are the methods that can’t be accessed outside the class in which they have been declared. These methods can only access the private variables of the class. You cannot use self-keyword with private methods. Private methods can be inherited with the help of the subclass. That subclass is eligible to access them as well as can override them too.

You can create private methods in the following manner,

    private
        def method
    end

Program:

=begin
  Ruby program to demonstrate private methods
=end	

class Example
  def call
    method1
  end
  
  private
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.call

Output

Corona Go! Go Corona

Explanation:

In the above code, you can observe that we cannot access private methods directly with the help of instance of the defined class. We have invoked the method inside a public class and then have invoked the public class.

2) Public methods

All methods present in the Ruby library are public by default. Even if you declare a method, that method is also public. Public methods are accessible by everyone. They are the methods that can be invoked outside the class.

You can define a public method with the help of public keyword in the following manner,

    public
	    def method
    end

Program:

=begin
  Ruby program to demonstrate public methods
=end	

class Example
  public
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.method1

Output

Corona Go! Go Corona

Explanation:

You can observe that public methods can be accessed directly without the help of any other method.

3) Protected methods

You can only invoke protected method with the help of instances which belongs to its defined class or its subclass. Protected methods are not accessible outside the defined class or subclass. There are finite uses of such methods.

You can define a protected method in the following way with the help of protected keyword,

    protected 
	    def method
    end

Program:

=begin
  Ruby program to demonstrate protected methods
=end	

class Example
  def call
    method1
  end
  
  protected
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.call

Output

Corona Go! Go Corona

Explanation:

In the above code, you can observe that we cannot access protected methods directly with the help of instance of the defined class. We have invoked the method inside a public class and then have invoked the public class.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.