class Example
private
def example_test
puts 'Hello'
end
end
e = Example.new
e.example_test
This of course will not work, because we specified explicit receiver - instance of Example (e
), and that is against a "private rule".
But I cannot understand, why one cannot do in Ruby this:
class Foo
def public_m
self.private_m # <=
end
private
def private_m
puts 'Hello'
end
end
Foo.new.public_m
The current object inside public_m
method definition (i.e. self
) is the instance of Foo. So why it is not allowed? To fix that I have to change self.private_m
to just private_m
. But why this differ, isn't the self
an instance of Foo inside public_m
? And who is the receiver of bare-word private_m
call? Isn't that self
- what actually you omit because, Ruby will do it for you (will call private_m on self)?
I hope I didn't confuse it too much, I am still fresh to Ruby.
EDIT:
Thank you for all the answers. Putting them all together I was able (finally) to grok the obvious (and not so obvious for someone, who have never seen things like Ruby): that self
itself can be
explicit and implicit receiver and that make the difference. So there are two rules, if you want to call a private method: self
must be implicit receiver, and that self must be an instance of current class (Example
in that case - and that takes place only when self if inside instance method definition, during this method execution). Please correct me if I am wrong.
class Example
# self as an explicit receiver (will throw an error)
def explicit
self.some_private_method
end
# self as an implicit receiver (will be ok)
def implicit
some_private_method
end
private
def some_private_method; end
end
Example.new.implicit
Message for anyone who could find this question during the google trails: this may be helpful - http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby
Does not exactly answer the Question, but you can call private methods this way
Adding some enhancements to User Gates solution. Calling a private method to class method or an instance method is pretty much possible. Here is the Code Snippets. But not recommended.
Class Method
Instance Method
Sorry for my prevoius answer. I just don't understand your question.
I changed your code like this:
Here is a call of instance method:
Here are a call of class methods:
The definition of
private
in Ruby is "can only be called without an explicit receiver". And that's why you can only call private methods without an explicit receiver. There is no other explanation.Note that there actually is an exception to the rule: because of the ambiguity between local variables and method calls, the following will always be resolved to be an assignment to a local variable:
So, what do you do if you want to call a writer called
foo=
? Well, you have to add an explicit receiver, because without the receiver Ruby simply won't know that you want to call the methodfoo=
instead of assigning to the local variablefoo
:But what do you do if you want to call a
private
writer calledfoo=
? You can't writeself.foo =
becausefoo=
isprivate
and thus cannot be called with an explicit receiver. Well, actually for this specific case (and this case alone), you can actually use an explicit receiver ofself
to call aprivate
writer.IIRC, private methods allow only implicit receiver (which is always self, of course).
Here's the short and the long of it. What private means in Ruby is a method cannot be called with an explicit receivers, e.g. some_instance.private_method(value). So even though the implicit receiver is self, in your example you explicitly use self so the private methods are not accessible.
Think of it this way, would you expect to be able to call a private method using a variable that you have assigned to an instance of a class? No. Self is a variable so it has to follow the same rules. However when you just call the method inside the instance then it works as expected because you aren't explicitly declaring the receiver.
Ruby being what it is you actually can call private methods using instance_eval:
Hope that's a little more clear.
-- edit --
I'm assuming you knew this will work: