Ruby Protected Method
last modified October 18, 2026
This tutorial explains Ruby's protected method visibility in depth.
Protected methods offer controlled access between related objects.
The protected modifier makes methods callable from within the class hierarchy. Unlike private methods, they can be called on another object when both objects are instances of the same class or classes related by inheritance. It sits between public and private access.
Called with no arguments, protected changes the visibility of all
methods defined after it in the class body.
Protected methods enable controlled sharing within class hierarchies. They're commonly used for internal comparisons and helper methods.
Basic Protected Method Example
This example demonstrates the fundamental protected method behavior. The method is accessible within the class hierarchy but not externally.
class Person
protected
def greet
"Hello from protected method"
end
end
class Employee < Person
def call_greet
greet # Works - same class hierarchy
end
end
person = Person.new
# person.greet # Error - protected method called
employee = Employee.new
puts employee.call_greet # Works through public method
The greet method is protected, so it can't be called directly on
person. However, Employee can access it through its
own public method.
Protected Method for Object Comparison
Protected methods are ideal for comparing objects of the same class. This example shows protected access between two Account objects.
class Account
attr_reader :balance
def initialize(balance)
@balance = balance
end
def >(other_account)
balance > other_account.balance
end
protected :balance
end
account1 = Account.new(1000)
account2 = Account.new(500)
puts account1 > account2 # true
# puts account1.balance # Error - protected method
The balance reader is protected, allowing comparison between
Account objects while preventing direct access. This maintains encapsulation.
Protected Access in Class Hierarchy
This example shows protected method access across a class hierarchy. Subclasses can access protected methods from parent classes.
class Vehicle
protected
def engine_details
"V6 Turbo Engine"
end
end
class Car < Vehicle
def show_engine
"Car has: #{engine_details}"
end
end
class Truck < Vehicle
def show_engine
"Truck has: #{engine_details}"
end
end
car = Car.new
puts car.show_engine
truck = Truck.new
puts truck.show_engine
Both Car and Truck can access the protected
engine_details method from their parent class. This promotes code
reuse while controlling access.
Protected Methods with Explicit Receivers
Protected methods can be called with an explicit receiver when the receiver is
an instance of the same class or a related class. This example transfers money
between two BankAccount objects.
class BankAccount
def initialize(balance)
@balance = balance
end
def transfer(other_account, amount)
if valid_transfer?(other_account, amount)
deduct(amount)
other_account.add(amount)
end
end
protected
def valid_transfer?(other_account, amount)
amount > 0 && @balance >= amount
end
def add(amount)
@balance += amount
end
def deduct(amount)
@balance -= amount
end
end
account1 = BankAccount.new(1000)
account2 = BankAccount.new(500)
account1.transfer(account2, 200)
The protected methods add and deduct can be called on
other BankAccount instances. This enables secure inter-object
operations while preventing external access.
Protected vs Private Methods
This example contrasts protected and private method behavior. The key difference is in receiver handling.
class Test
def call_protected(other)
other.protected_method
end
def call_private(other)
other.private_method # Error - private method
end
protected
def protected_method
"Protected called"
end
private
def private_method
"Private called"
end
end
test1 = Test.new
test2 = Test.new
puts test1.call_protected(test2) # Works
# test1.call_private(test2) # Fails
Protected methods allow an explicit receiver when it is an instance of the same
class or a related class. Private methods allow the literal self
receiver but reject calls on another object. That is the fundamental
distinction between them.
Protected Class Methods
Protected visibility can also be applied to class methods. This example shows protected class method usage.
class Configuration
class << self
protected
def database_settings
{ adapter: 'postgresql', host: 'localhost' }
end
end
def self.get_settings
database_settings # Accessible within class
end
end
puts Configuration.get_settings
# Configuration.database_settings # Error - protected method
The protected class method database_settings is accessible within
the class but not externally. This pattern is useful for internal class logic.
Protected Methods in Modules
Protected methods work similarly in modules. This example demonstrates protected method behavior in an included module.
require 'securerandom'
module Authenticable
protected
def generate_token
SecureRandom.hex(20)
end
end
class User
include Authenticable
def login
@token = generate_token
"Logged in with token: #{@token[0..5]}..."
end
end
user = User.new
puts user.login
# user.generate_token # Error - protected method
The protected generate_token method from the module is accessible
within User but not externally. This maintains security while
allowing method sharing.
Source
This tutorial covered Ruby's protected method visibility with practical examples showing object comparison, class hierarchies, and module usage.
Author
List all Ruby tutorials.