When learning Ruby with a background in Java/C# .NET and similar high level object oriented programming languages method visibility can be a major stumbling block. To start things off all Ruby methods are by default public or accessible by any object. To limit access you have the option of modifying a methods visibility to either protected or private. Before we dive into what they mean lets look at the two ways to modify method visibility.
Modifying Method Visibility
To begin lets work with sample class that has 3 methods
class SampleClass def methodOne end def methodTwo end def methodThree end end
By default all the methods of this class are public and accessible by anyone. To modify the method scope we need to use the private or protected modifier methods / keywords.
To define a single method or methods as private/protected you simply
pass a comma separated list of named methods using the syntax :methodName.
class SampleClass def methodOne end def methodTwo end def methodThree end private :methodTwo, :methodThree end
Methods Following Keyword
If the private/protected method modifiers are used without named methods then any method defined after the keyword will have that scope. For example both methodTwo and methodThree in the below example will be protected.
class SampleClass def methodOne end protected def methodTwo end def methodThree end end
Private in Ruby means the method is private to an instance of the Object. What this means is that if you mark a method as private that method still exists for classes that inherit off the object but an instance of one object can not call the private method of another instance. This varies from Java in that private methods are not available in sub classes.
Another item to remember is that private methods can only be called implicitly and not by specifying the receiver.
class SampleClass def methodA end private :methodA # defined methodA as private def callMethodA methodA # method call successfully self.methodA # exception, you can not call a private method by defining the instance calling it even if it is self obj.methodA # exception an instance can not access the private method of another object even if it is of the same class end end
As you can see private methods in Ruby are inheritable by sub classes but can only be called in the context of the calling instance.
Protected methods in Ruby share the same inheritability of Private methods. However protected methods can be accessed explicitly by other instances of the object.
class SampleClass def test( obj) self.var1 = obj.myTestMethod end protected def myTestMethod "hello" end end
Pulling it all together
To make Method Visibility easier to understand one must understand that both private and protected methods are inherited by sub classes. The difference simply being that private methods can only be called in the context of an instance without defining an explicit caller of self. On the other hand protected methods are accessible from instances of the same class or inheriting sub classes.