Reputation: 27862
I am confused on how this library works:
The ApplicationController has a private method called selected_account()
as you can see here: ApplicationController
Then, from another Controller which is a child from ApplicationController, we do an action that does this:
def index()
@selected_account = selected_account
graph = get_accounts_graph()
@accounts = Account.get_accounts_map(graph)
end
How can we do that? Isn't it out of scope?
Upvotes: 1
Views: 2512
Reputation: 29369
This will be confusing for those who come from java or c# world. But here is a decent explanation
In Ruby, the inheritance hierarchy or the package/module don't really enter into the equation, it is rather all about which object is the receiver of a particular method call. When a method is declared private in Ruby, it means this method can never be called with an explicit receiver. Any time we're able to call a private method with an implicit receiver it will always succeed. This means we can call a private method from within a class it is declared in as well as all subclasses of this class
Upvotes: 2
Reputation: 3040
It might be confusing at first, but in Ruby private
does not mean what it means in Java/C++. Ancestors can call private methods just fine.
What private
actually means is that you cannot call the method with an explicit target. That is, if foo
is private, you can call it with foo()
, but not with self.foo()
or obj.foo()
. That way you can call private methods only on yourself.
Furthermore, Ruby has protected
. The semantics are again different from Java/C++ – protected
methods can only be called from instances of the same class. It is rarely used.
Upvotes: 2
Reputation: 13106
The child controller inherits from ApplicationController so it can call internal private methods on itself without issue. There is no scoping problem. The subclass inherits all the public/private/protected methods of its super class.
Upvotes: 0