What is the difference between private, protected, and public methods in Ruby?
Updated May 17, 2026
Short answer
public methods can be called by anyone. private methods can only be called implicitly without an explicit receiver (except self in modern Ruby). protected methods can be called by instances of the same class or its subclasses.
Deep explanation
Public methods represent an object's external API interface. Private methods are internal helpers; they cannot be invoked with an explicit receiver object (e.g., object.private_method fails). In modern Ruby (2.7+), calling a private method with self. as an explicit receiver is allowed. Protected methods are specialized: an instance of a class can call a protected method on another instance of that same class or child hierarchy class, making it perfect for internal data alignment.
Real-world example
Using protected methods when implementing sorting or comparison strategies where one instance needs to read internal sensitive attributes (other_object.secret_score) of an identical type.
Common mistakes
- Believing that `private` prevents subclass inheritance. Subclasses in Ruby inherit private methods fully and can call or override them freely.
Follow-up questions
- Can you call a private method from outside the class entirely?
- How do you make class-level methods private?