Ruby Private Method
last modified October 18, 2026
This tutorial explains how to use Ruby's private method modifier.
Private methods are an essential part of object-oriented programming in Ruby.
The private modifier makes methods callable only from inside the
class or its subclasses. They cannot be called on another object, but they can
be called implicitly or with the literal receiver self.
Called with no arguments, private changes the visibility of all
methods defined after it in the class body.
Private methods enforce encapsulation by hiding implementation details. They help create clean interfaces and prevent misuse of internal class functionality.
Basic Private Method Example
This simple example demonstrates the basic usage of private methods in Ruby. The private method is called from another instance method and cannot be called on the object from outside.
class Person
def greet
puts "Hello, #{name}"
end
private
def name
"John Doe"
end
end
person = Person.new
person.greet
# person.name # This would raise NoMethodError
The name method is private and is called from greet.
Attempting to call it directly on an instance raises a NoMethodError.
Private Method Calling Convention
Private methods are normally called with an implicit receiver. A literal
self receiver is also allowed, but calling a private method on
another object raises a NoMethodError.
class Calculator
def add(a, b)
validate(a)
validate(b)
a + b
end
private
def validate(num)
raise "Not a number" unless num.is_a?(Numeric)
end
end
calc = Calculator.new
puts calc.add(5, 3)
# calc.validate(5) # This would raise NoMethodError
The validate method is private and used internally by add.
It cannot be called directly on a Calculator instance.
Private Class Methods
To make class methods private, we need to use the private_class_method
method or define them within a class << self block.
class Logger
def self.log(message)
format_message(message)
end
private_class_method def self.format_message(msg)
"[#{Time.now}] #{msg}"
end
end
puts Logger.log("Test message")
# Logger.format_message("Test") # This would raise NoMethodError
The format_message class method is private and can only be called
internally by other class methods.
Private Methods in Inheritance
Private methods are inherited by subclasses but remain private. They can be called by subclass methods but not directly on instances.
class Animal
def speak
make_sound
end
private
def make_sound
"Generic animal noise"
end
end
class Dog < Animal
def bark
make_sound + " - Woof!"
end
end
dog = Dog.new
puts dog.bark
# dog.make_sound # This would raise NoMethodError
The Dog class inherits the private make_sound method from Animal.
It can call it internally but not expose it directly.
Private Methods with send
Ruby's send method can bypass private method restrictions. This
should be used cautiously as it breaks encapsulation.
class Secret
private
def hidden_method
"Top secret information"
end
end
secret = Secret.new
# puts secret.hidden_method # Would raise NoMethodError
puts secret.send(:hidden_method)
While send can access private methods, this is generally considered
bad practice. It should only be used in special cases like testing.
Private Methods in Modules
Modules can define private methods that become private when included in classes. This is useful for creating reusable private functionality.
module Validations
def validate_presence(value)
raise "Cannot be blank" if value.nil? || value.empty?
end
private :validate_presence
end
class User
include Validations
def save(name)
validate_presence(name)
puts "User saved"
end
end
user = User.new
user.save("John")
# user.validate_presence("") # This would raise NoMethodError
The validate_presence method is private in the module and remains
private when included in the User class.
Private Setter Methods
Setters must be called with an explicit receiver, because a bare
balance = value would create a local variable instead of calling
the setter. Ruby permits self.balance = value even when the setter
is private, so private accessors remain usable inside the class.
class BankAccount
def initialize(balance)
self.balance = balance
end
def deposit(amount)
self.balance += amount
end
private
attr_reader :balance
attr_writer :balance
end
account = BankAccount.new(100)
account.deposit(50)
# account.balance = 200 # This would raise NoMethodError
The balance setter is private, yet it remains usable inside the class through
self.balance. The reader is also private, so external code cannot
read or write the balance.
Source
This tutorial covered Ruby's private methods with practical examples showing their usage in classes, modules, inheritance, and special cases.
Author
List all Ruby tutorials.