Ruby self Keyword
last modified October 18, 2026
This tutorial explains how to use Ruby's self keyword. The
self keyword is fundamental to Ruby's object-oriented nature.
The self keyword refers to the current object, the default receiver of method calls. It changes context depending on where it's used in the code.
Understanding self is crucial for writing idiomatic Ruby code. It
affects method definitions, class methods, and instance variable access.
Basic self in Instance Methods
In instance methods, self refers to the instance of the class.
This example shows how to use self to access instance variables.
class Person
def initialize(name)
@name = name
end
def introduce
puts "Hello, my name is #{self.name}"
end
def name
@name
end
end
person = Person.new("Alice")
person.introduce
Here self.name calls the name getter method on the current instance.
The self keyword makes it explicit that we're calling a method.
self in Class Methods
In class method definitions, self refers to the class itself.
This is how Ruby defines class-level methods.
class Logger
def self.info(message)
puts "[INFO] #{message}"
end
def self.error(message)
puts "[ERROR] #{message}"
end
end
Logger.info("Application started")
Logger.error("Something went wrong")
The self. prefix defines methods on the class itself rather than
instances. These are called directly on the class.
self for Method Chaining
Returning self from methods enables method chaining. This creates
a fluent interface where methods can be called in sequence.
class Calculator
def initialize(value = 0)
@value = value
end
def add(num)
@value += num
self
end
def subtract(num)
@value -= num
self
end
def result
@value
end
end
calc = Calculator.new
puts calc.add(5).subtract(2).add(10).result
Each method returns self, allowing calls to be chained together.
The final result call returns the computed value.
self in Module Definitions
Inside a module body, self is the module itself, so
def self.square defines a method on the module.
module MathUtils
def self.square(x)
x * x
end
def self.cube(x)
x * x * x
end
end
puts MathUtils.square(5)
puts MathUtils.cube(3)
Module methods defined with self are called directly on the module.
They don't require including the module in a class.
self for Explicit Receiver
Using self as an explicit receiver clarifies method calls and
distinguishes between local variables and method calls.
class BankAccount
attr_accessor :balance
def initialize(balance = 0)
@balance = balance
end
def deposit(amount)
self.balance += amount
end
def withdraw(amount)
self.balance -= amount
end
end
account = BankAccount.new(100)
account.deposit(50)
account.withdraw(30)
puts account.balance
Here self.balance calls both the getter and setter used by
+=. Without self, Ruby would treat balance
as a local variable and the assignment would fail.
self in Singleton Methods
self is used to define singleton methods on individual objects.
These methods exist only for that specific instance.
greeting = "Hello, world"
def greeting.shout
self.upcase + "!"
end
puts greeting.shout
puts greeting.upcase
begin
"Another string".shout
rescue => e
puts "Error: #{e.message}"
end
The shout method is only available on the greeting
object. Other strings don't have this method, as shown by the error.
self in Class Definitions
Inside a class definition but outside methods, self refers to
the class being defined. This is used for metaprogramming.
class Animal
puts "In Animal class definition, self is #{self}"
def self.species
puts "Species method called on #{self}"
end
def initialize
puts "In instance method, self is #{self}"
end
end
Animal.species
animal = Animal.new
The output shows how self changes context. In the class body and
class method, it's the class itself; in the instance method, it's the instance.
self in Blocks
An ordinary block keeps the self of the code that created it.
Inside an instance method, self in a block still refers to the
instance.
class Counter
def initialize
@count = 0
end
def run
3.times do
self.increment
end
self
end
def increment
@count += 1
end
def count
@count
end
end
puts Counter.new.run.count
The block passed to times calls self.increment on the
same Counter instance. Unlike a method defined with
def, a block does not introduce a new self.
Source
This tutorial covered Ruby's self keyword with practical examples showing instance methods, class methods, method chaining, and more.
Author
List all Ruby tutorials.