Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, access to methods is controlled by three keywords: public, private, and protected. These keywords are used to control the visibility of methods in a class (encapsulation).
Public (Public Methods)
Regarding public methods, there are a few key points to remember. Methods declared as public can be called by anyone - both inside and outside the class. By default, all methods in a class are public. You can specify that a method is public, but for brevity and simplicity, this is not usually done.
class MyClass
def public_method_name
# This method is public by default
end
public :public_method_name # Explicitly declaring it as public, but it's not necessary
end
class MyClass
def public_method_name
# This method is public by default
end
end
Technically, the examples are identical - public_method_name is a public method.Β
Private (Private Methods)
Methods declared as private can only be called within the class where they are defined. They cannot be called from outside the class, even from an instance of that class.
class MyClass
private # all methods that follow this line are private
def private_method_name
# This method is private
end
end
Protected (Protected Methods)
Methods declared as protected can only be called within the same class or instances of its subclasses. It is similar to private access, but allows access for subclasses.
class MyClass
protected # all methods that follow this line are protected
def protected_method
# This method is protected
end
end
Questions about public, private, and protected are quite common in interviews. This information is best absorbed when learned through examples/exercises. For instance:
Exercise to Learn Public, Private, and Protected Methods
Create a class "User":
- Create a User class with the following properties: name and age.
- All properties should be private.
Add Methods:
- Add a greet method that outputs the message: "Hello, how are you?"
- Add an access_age method that returns the user's age.
Change Access Levels:
- Make the greet method public.
- Make the access_age method protected.
Create a subclass "Admin":
- Create a subclass Admin that inherits from the User class.
Test the Implementation:
- Create an object of the User class and an object of the Admin class.
- Call the greet method on the objects of both classes.
- Try to access the age using the access_age method on the User class object and try the same on the Admin class object.
Record Your Observations:
- Note which methods you successfully called and which resulted in an error.
- Determine which methods can be called from outside the class and which cannot.
Optimize the Code (Refactoring)
- Organize the code
- Change method names to more understandable ones (access_age -> age)
Here is a skeleton of code that may help you get oriented:
class User
def initialize(name, age)
@name = name
@age = age
end
def greet
puts "Hello, how are you?"
end
def access_age
@age
end
private
def private_method
puts "This is a private method"
end
protected
def protected_method
puts "This is a protected method"
end
end
class Admin < User
# Admin inherits all properties and methods from User
end
# Creating objects
user = User.new("User", 25)
admin = Admin.new("Admin", 30)
# Calling the public method
user.greet
admin.greet
# Trying to call the protected method
# user.protected_method # Will result in an error
# Trying to call the private method
# user.private_method # Will result in an error
# Output age
puts "User's age: #{user.access_age}"
puts "Admin's age: #{admin.access_age}"
If this exercise seems challenging - come back to it after some time. Review how classes work, what initialize is, what an instance of a class is, etc.
Π―ΠΊΡΠΎ Π²ΡΡ ΠΌΠ΅ΡΠΎΠ΄ΠΈ ΠΌΠ°ΡΠΈΠΌΡΡΡ ΠΌΠΎΠ΄ΠΈΡΡΠΊΠ°ΡΠΎΡ Π΄ΠΎΡΡΡΠΏΡ public, ΡΠ΅ ΠΏΡΠΈΠ·Π²Π΅Π΄Π΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΡΠΎ Π²ΡΡ ΡΠ½ΡΡ ΠΊΠ»Π°ΡΠΈ ΡΠ° ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠΈ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΈ ΠΌΠ°ΡΠΈΠΌΡΡΡ Π±Π΅Π·ΠΏΠ΅ΡΠ΅ΡΠΊΠΎΠ΄Π½ΠΈΠΉ Π΄ΠΎΡΡΡΠΏ Π΄ΠΎ ΡΠΈΡ
ΠΌΠ΅ΡΠΎΠ΄ΡΠ².
Π¦Π΅ ΠΌΠΎΠΆΠ΅ ΠΏΡΠΈΠ·Π²Π΅ΡΡΠΈ Π΄ΠΎ Π½Π΅ΠΏΠ΅ΡΠ΅Π΄Π±Π°ΡΡΠ²Π°Π½ΠΈΡ
ΠΊΠΎΠ½ΡΠ»ΡΠΊΡΡΠ² Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΡ.
Π ΡΠ½ΡΠΎΠ³ΠΎ Π±ΠΎΠΊΡ, ΡΠ΅ ΠΌΠΎΠΆΠ΅ ΠΏΠΎΠ»Π΅Π³ΡΠΈΡΠΈ Π²Π·Π°ΡΠΌΠΎΠ΄ΡΡ ΠΌΡΠΆ ΠΊΠ»Π°ΡΠ°ΠΌΠΈ ΡΠ° ΡΠΏΡΠΈΡΡΠΈ ΡΠ²ΠΈΠ΄ΠΊΠΎΡ ΡΠΎΠ·ΡΠΎΠ±ΡΡ, Π°Π»Π΅ ΡΠ°ΠΊΠΈΠΉ ΠΏΡΠ΄Ρ
ΡΠ΄ Π½Π΅ Π·Π°Π²ΠΆΠ΄ΠΈ Ρ Π±Π΅Π·ΠΏΠ΅ΡΠ½ΠΈΠΌ Ρ ΠΌΠΎΠΆΠ΅ ΠΏΠΎΡΠΎΠ΄ΠΆΡΠ²Π°ΡΠΈ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠΈ Π·Ρ Π·Π±Π΅ΡΠ΅ΠΆΠ΅Π½Π½ΡΠΌ Π΄Π°Π½ΠΈΡ
ΡΠ° ΠΊΠΎΠ½ΡΡΠ΄Π΅Π½ΡΡΠΉΠ½ΡΡΡΡ.