Method Access Levels in Ruby
A common point of confusion on method access levels for some programmers who just started in Ruby. There are three access levels: public, protected, and private.
- Public methods can be called by anyone—no access control is enforced. Methods are public by default (except for initialize, which is always private).
- Protected methods can be invoked only by objects of the defining class and its sub-classes. Access is kept within the family.
- Private methods cannot be called with an explicit receiver—the receiver is always the current object, also known as self. This means that private methods can be called only in the context of the current object; you can’t invoke another object’s private methods.
The difference between “protected” and “private” is fairly subtle and is different in Ruby than in most common OO languages. If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.
Suppose you have two instances of the Foo class, A and B. In languages like C#, A and B can call each other’s private methods. In Ruby, you need to use a protected method for that. This is the main difference between private and protected methods in Ruby.
Suppose further that you have a class called Foo and a subclass SubFoo. In languages like C#, SubFoo has no access to any private methods defined by Foo. In Ruby, it provides no way to hide a class’s methods from its subclasses. In this way, Ruby’s private works like C#’s protected.
To demonstrate these differences, let’s see in a example below:
class Foo
def public_default_bar
"public"
end
protected
def protected_bar
"protected"
end
private
def private_bar
"private"
end
def private_bar=(bar)
@bar = "private"
end
public
def public_bar
"public"
end
end
class SubFoo < Foo
def check_protected_method_explicit_receiver
self.protected_bar #it works
end
def check_protected_method_implicit_receiver
protected_bar #it works
end
def check_private_method_explicit_receiver
self.private_bar #it doesn't work
end
def check_private_method_implicit_receiver
private_bar #it works
end
def check_set_private_method_explicit_receiver
self.private_bar = "foo" #it works, specail case only for set method
end
def check_set_private_method_implicit_receiver
private_bar = "foo" #it doesn't call the method, it's just a local variable assignment here
end
end
I put the comments on each method in SubFoo to clarify which method call works, but you can also instantiate object from SubFoo in irb and call each method to see the result. Hopefully, you get understand and use it correctly when you do class design.
2 people have left comments
CAMERON said:
Medicamentspot.com International Legal RX Medications. Special Internet Prices (up to 40% off average US price). NO PRIOR PRESCRIPTION REQUIRED!…
Combivir@buy.online” rel=”nofollow”>.…
- Copyright 2010 WowKhmer Tech. All Rights Reserved. Powered by Wordpress | Theme designed by Chris Murphy
- Back To Top
- Home








Leave a Comment-