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,
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,
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,
From outside public class methods would be accessible as -
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.
Now if we try to access,
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,
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
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.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox