What are the three levels of access control for methods in Ruby?

Beginner

Answer

  • public: Can be called from anywhere (default)
  • protected: Can be called by instances of the same class or subclasses
  • private: Can only be called within the same instance (no explicit receiver)
class MyClass
  def public_method; end
  
  protected
  def protected_method; end
  
  private
  def private_method; end
end