0
0
Rubyprogramming~10 mins

Protected and private visibility in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protected and private visibility
Define Class with Methods
Call Public Method
Works: Accessible Anywhere
Call Protected Method
Works: Accessible within class and subclasses, and between instances
Call Private Method
Works: Accessible only within the defining class, no explicit receiver
Attempt Outside Access
Error if Protected/Private Accessed Improperly
Shows how Ruby controls method access: public methods are open, protected methods are accessible within class and subclasses, private methods only inside the class without explicit receiver.
Execution Sample
Ruby
class Person
  def public_info
    'Public info'
  end

  protected
  def protected_info
    'Protected info'
  end

  private
  def private_info
    'Private info'
  end
end

p = Person.new
puts p.public_info
puts p.protected_info # Error
puts p.private_info   # Error
Defines a class with public, protected, and private methods and tries to call them from outside.
Execution Table
StepActionMethod CalledAccess CheckResult/Output
1Create instance p--Person object created
2Call p.public_infopublic_infoPublic method, accessible'Public info' printed
3Call p.protected_infoprotected_infoProtected method, called from outsideError: protected method called
4Call p.private_infoprivate_infoPrivate method, called with explicit receiverError: private method called
5End of program--Execution stops due to errors
💡 Execution stops because protected and private methods cannot be called with explicit receiver from outside.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
pnilPerson instancePerson instancePerson instancePerson instancePerson instance
Key Moments - 3 Insights
Why does calling p.protected_info cause an error?
Because protected methods cannot be called with an explicit receiver from outside the class, as shown in execution_table step 3.
Why can't we call p.private_info even though p is an instance of the class?
Private methods cannot be called with an explicit receiver, even if it's the same class instance, as shown in execution_table step 4.
Can protected methods be called between instances of the same class?
Yes, protected methods can be called between instances of the same class or subclasses, but not from outside, which is why step 3 fails.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when calling p.public_info at step 2?
AError: private method called
B'Public info' printed
CError: protected method called
DNo output
💡 Hint
Check execution_table row with Step 2 for method call and result.
At which step does the program raise an error due to calling a private method with an explicit receiver?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at execution_table rows for private_info call and error.
If we call protected_info from inside another method of the same class without an explicit receiver, what would happen?
AIt would return nil
BIt would raise an error
CIt would work and return 'Protected info'
DIt would call private_info instead
💡 Hint
Recall that protected methods are accessible within the class and subclasses without explicit receiver.
Concept Snapshot
Ruby method visibility:
- public: accessible anywhere
- protected: accessible within class, subclasses, and between instances
- private: accessible only inside class, no explicit receiver
Calling protected/private methods with explicit receiver outside class causes error.
Full Transcript
This example shows how Ruby controls access to methods using public, protected, and private keywords. Public methods can be called from anywhere, so calling p.public_info works fine. Protected methods can only be called within the class or subclasses, or between instances of the same class, but not from outside with an explicit receiver, so p.protected_info causes an error. Private methods are even more restricted; they cannot be called with an explicit receiver, even from the same class instance, so p.private_info also causes an error. Understanding these rules helps avoid access errors in Ruby programs.