Recall & Review
beginner
What does private visibility mean in Ruby?
Methods marked as private can only be called within the same object. They cannot be called with an explicit receiver, even if it's self.
Click to reveal answer
intermediate
What is the difference between protected and private methods in Ruby?
<strong>Protected</strong> methods can be called by any instance of the defining class or its subclasses, but only within the same family of objects. <strong>Private</strong> methods can only be called without an explicit receiver inside the same object.Click to reveal answer
beginner
How do you declare a method as private in Ruby?
You can declare a method as private by placing it below the keyword
private in the class, or by calling private :method_name.Click to reveal answer
intermediate
Can a <strong>protected</strong> method be called from outside the class in Ruby?No, <strong>protected</strong> methods cannot be called from outside the class or its subclasses unless the call is made between instances of the same class or subclass.Click to reveal answer
intermediate
Why use protected methods instead of private methods?
Use <strong>protected</strong> when you want to allow access to methods between instances of the same class or subclasses, but keep them hidden from outside objects. <strong>Private</strong> is stricter and only allows calls within the same object.Click to reveal answer
Which of the following is true about private methods in Ruby?
✗ Incorrect
Private methods in Ruby can only be called without an explicit receiver inside the same object.
What keyword do you use to declare protected methods in Ruby?
✗ Incorrect
The keyword
protected is used to declare protected methods in Ruby.Can a protected method be called by another instance of the same class?
✗ Incorrect
Protected methods allow access between instances of the same class or subclasses.
Which visibility level is the most restrictive in Ruby?
✗ Incorrect
Private methods are the most restrictive because they cannot be called with an explicit receiver, even within the same class.
If you want a method to be accessible only within the same object, which visibility should you use?
✗ Incorrect
Private methods are accessible only within the same object without an explicit receiver.
Explain the difference between protected and private visibility in Ruby with an example.
Think about who can call the methods and from where.
You got /4 concepts.
Describe a real-life situation where using protected methods would be better than private methods.
Consider a family or team where members share some secrets but keep others private.
You got /3 concepts.