MDBitz - Matthew Denton
Ruby
Calling an object method in Ruby behind the scenes is actually a message to another object or Message Passing. These can more easily be understood by an example, and for that reason lets look at a basic multiplication script.
3 x 4
</ruby>
As you are aware everything in Ruby is an object, including an integer. Therefor the above operation is really a call to the <em>x</em> method of the integer object.
<pre lang="ruby">
3.x(4)
So far this isn’t very surprising as you are calling the multiplication method of the integer object, however you can call the same method by using the send function passing the method name and parameters. This is called Message Passing.
You may wonder why does it matter. The answer simply put is that you can call any method of an object regardless of its visibility by using the send method. If you want to call a private method of an object that you normally wouldn’t be able to call then by using send you can do so. Ruby has actually tried to restrict this functionality but has been forced to retain it and instead also now provides a public_send method in version 1.9 for those cases you don’t want to access private methods.
Seriously what is the point to having Method Visibility if the underlying fundamentals of the language itself allow developers to bypass them. A ball was definitely dropped somewhere during the creation of the Ruby language.
message passing, Method Visibility, My Self-Inflicted crash course introduction into Ruby, Ruby
MDBitz - Matthew Denton
Ruby
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.
Named Methods
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 Methods
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
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.
Access Control, Method Visibility, My Self-Inflicted crash course introduction into Ruby, private, protected, public, Ruby