liveBook · Manning

Self

As explained in The Well-Grounded Rubyist, Fourth Edition

self is Ruby's implicit reference to the object that is the current method-execution receiver.

In `puts "Hello"`, there is no object to the left of a dot, but `puts` is still a method call. Ruby sends it to `self`, the current object. Exactly one object plays that role at any point in execution; which object it is depends on the context. section 1.1.4 section 5.1

Follow the context to find `self`:

  • At the top level, `self` is the built-in object that identifies itself as `main`. `main` is not a name you can use to retrieve that object; assign `self` to a variable if you need to keep it. section 5.1.2
  • Inside a class or module definition body, `self` is that class or module object. Leaving a nested definition restores the surrounding context. section 5.1.3
  • While a method runs, `self` is its receiver. An instance method uses the instance that received the call; a singleton method uses the particular object on which it was defined. For a class method, that object is the class. section 5.1.3
Table 5.1 How the current self object is determined

Context

Example

Which object is self?

Top level of program

Any code outside of other blocks

main (built-in top-level default object)

Class definition

class C

self

The class object C

Module definition

module M

self

The module object M

Method definitions

1. Top level (outside any definition block):

def method_name

self

Whatever object is self when the method is called; top-level methods are available as private methods to all objects

2. Instance-method definition in a class:

class C

def method_name

self

An instance of C, responding to method_name

3. Instance-method definition in a module:

module M

def method_name

self

Individual object extended with M

Instance of class that mixes in M

4. Singleton method on a specific object:

def obj.method_name

self

obj

Use the context table to check which object receives an implicit method call, especially when moving between definition bodies and method calls. section 5.1.1

A method calls another method

With `class C; def x; puts "x"; end; def y; x; end; end`, calling `C.new.y` runs `x` on that same instance. The call inside `y` needs no dot because its receiver is already `self`. By contrast, at the top of a class definition, a bare call such as `attr_reader :name` goes to the class object. section 5.1.4 section 3.4.1

A bare identifier can also name a local variable. If a local variable and method share a name, the variable wins; `self.talk` or `talk()` makes the method call explicit. Avoiding the clash is simpler. section 5.1.4

Instance variables belong to whichever object is `self` when they are evaluated. Thus `@v` in a class body belongs to the class object, while `@v` in an instance method belongs to the instance that calls it: identical spelling does not make them the same variable. This differs from local-variable scope, which can change without `self` changing. section 5.1.5 section 5.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through top-level, class, module, instance-method, and singleton-method contexts, including receiver and variable-resolution edge cases.
Examples
Uses `puts`, a class method called without a dot, nested definitions, and matching instance-variable names on different objects.
What it assumes
Assumes basic familiarity with Ruby objects and method calls while establishing how their receiver changes across contexts.
What you can do after
Diagnose/debug with it by identifying a call’s implicit receiver and the object that owns an instance variable.
How it connects
Connects method receivers and class objects to instance variables, local-variable scope, and singleton methods.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.