I have looked this up countless times in the last three months. And I am determined to get this straight.
All methods are public in ruby by default.
Private means the method is private to this instance. Private methods can not be called with explicit receivers.
Protected means the method is available to any instance of the class and is also available to the children of that class and subclass. Protected methods can be called with explicit receivers.
In the example below self.a and other_object.a are examples of explicit receivers. Therefore self.a and other_object.a would work if the method was protected. However, because “a” is a private method, these explicit receivers will not work.
See example from Wikipedia:
To truly understand the difference I had to do a bit of research on the difference between implicit and explicit receivers. Which I understood after reading Matt Hodan’s example:
"We’ve created a “Parent” class and a “Child” class that is a sub-class of Parent. Parent has a private method “name” that returns the value “Mommy”. When we call “Child.new.get_parent_name” on line 18, we see the output produced by lines 12-14. Only when the Parent’s method “name” is called with an implicit receiver do we get a return value.
What does this mean? This means that private methods can only be called within the context of the current object (since any context outside of the current object would require an explicit receiver). I.e. one object cannot access another object’s private methods.”
If you were to change Private to Protected in the above two examples, then all three instances of explicit and implicit methods would work!