Access specifiers in Ruby

This tutorial will discuss how to define Access specifiers in Ruby and what do they mean for classes and modules where they are being used.

Default Scope

Whenever you define a new method in any class/module the default access specifier that Ruby will use is public.

Thus, all methods defined without any scope are public by default.

1. public

Methods having public scope can be called from anywhere i.e. from outside class as well.

1) public access to instance methods

If public access to instance method is given then those can be accessed from outside of the class but with instance of the class.

One can define scope for methods being defined as public explicitly as well. This can be done as given below,

class TestClass

  public

  def some_method_a
    # method body
  end

  def some_method_b
    # method body
  end
end

The above example shows that access specifier public is applied on methods defined after the access specifier is declared, resulting methods some_method_a and some_method_b getting public access.

These methods can be called from any class outside TestClass as given below,

test_objet = TestClass.new
test_object.some_method_a
test_object.some_method_b

This would work as these methods were having public access.

2) public access to class(self) methods

One can give public access to class methods as well.

Now you would wonder what does public access to private methods achieve?

But they certainly achieve privacy of class methods, following sample example will illustrate this,

class User
  def instance_method_first
    #some code
  end

  def self.class_method_first
    # some code
  end

  def self.class_method_second
    # some code
    class_method_first
  end
end

From outside public class methods would be accessible as -

User.class_method_first
or
User.class_method_second

As we know by default all methods will have public scope. The instance method instance_method_first and class methods class_method_first and class_method_second will have a public scope and they can be called from anywhere. We can see that they can be called from themselves as well. e.g. class_method_second has called class_method_first.

2. private

1) private class methods in ruby

Question?

Can we make class methods private? If yes, how?

Yes, We can make class methods private using access specifier private_class_method

What does this mean -

This means that private class methods would be accessible to the methods defined in the same class only. They can not be called directly from outside of the class. If they need to be accessed from outside of the class, then there needs to be a wrapper method in the class that has public scope which can ultimately call a private method.

e.g.

class User
  def some_instance_method
    # some body
  end

  def self.method_first
    # some body
  end

  def self.method_second
    # calling method_first
    method_first
  end

  private_class_method :method_first
end

Now if we try to access,

User.method_first
NoMethodError: private method `method_first' called for User:Class

Then it would give error as show above as it is private and can be accessed from the User class only.

If we try to access,

User.method_second

Then it would work properly as expected.

This way you can make class methods private in ruby.

2) private instance methods in ruby

You can define private instance methods in ruby by specifying private access specifier before defining your instance methods. The private access specifier will be applied on all the methods defined after it’s declaration.

e.g

class User
  def some_public_method
    # code
  end

  private

  def method_first
    # code
  end

  def method_second
    # code
  end
end

Here both instance methods method_first and method_second will act as private methods. Thus these methods can be called from the instance methods of the same class only. These would not be accessible from outside of the class.