Singleton methods and class << self

def self.x and class << self

A class method is written with def self.x when there is one of them, and inside class << self when there are several:

class Counter def self.start 0 end end class Clock class << self def now Time.now end def today now.strftime("%Y-%m-%d") end end end p Counter.start p Clock.today

ruby -e 'class Counter; def self.start = 0; end; class Clock; class << self; def now = Time.now; def today = now.strftime("%Y-%m-%d"); end; end; p Counter.start; p Clock.today'

0
"2026-12-02"

The date is the day the program ran, so it changes from one day to the next. The second form is the same thing with the receiver self (the class) opened once. It is what a class uses to define a helper that another class method calls, and it is the only way to call private or attr_accessor on class-level state.

Methods on one object

The same mechanism works on a single object, not just on a class. A method defined with the object as the receiver belongs to that object alone:

ruby -e 'o = Object.new; def o.hi = :hi; p o.hi'

:hi

ruby -e 'o = Object.new; def o.hi = :hi; p o.singleton_methods'

[:hi]

singleton_methods lists what was added this way. In application code a per-object method is rare and usually a test double: it replaces one method on one object without touching the class.

Where a class method lives

A class is an object, and its class methods live on its singleton class. A.singleton_class prints as #<Class:A>, and the methods defined there are the ones A.x calls:

ruby -e 'class A; class << self; def x = 1; end; end; p A.singleton_class'

#<Class:A>

ruby -e 'class A; class << self; def x = 1; end; end; p A.singleton_class.instance_methods(false)'

[:x]

That is why private before def self.x does not hide the method from callers, and why private_class_method :x does. The visibility keywords act on the class the method is being defined in, and a class method is one level up.

What to watch for

A class method that needs an instance usually wants to be an instance method, or a factory that names what it builds: User.from_row(row). A class full of self. methods that take the state as arguments is a module in disguise.

class << self is not a scope for local variables: a local defined inside it is gone after the end. Class-level state lives in an instance variable of the class, @count, not in a local.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does def self.x inside a class define?

    Answer

    A class method on the class object

    self in the class body is the class, so the method belongs to the class object.

  2. What does this program print? class A def self.x 1 end end p A.x

    Answer

    1

    A.x calls the class method and p prints its value.

  3. What does this program print? o = Object.new def o.hi = :hi p o.hi

    Answer

    :hi

    The method belongs to that one object, and p prints the symbol as :hi.

  4. What does this program print? o = Object.new def o.hi = :hi p o.singleton_methods

    Answer

    [:hi]

    singleton_methods lists exactly what was added to this object.

  5. What does this program print? class A class << self def x = 1 end end p A.singleton_class

    Answer

    #<Class:A>

    A class is an object, and its singleton class prints as #<Class:A>.

  6. What does this program print? class B class << self def a = 1 def b = a + 1 end end p B.b

    Answer

    2

    Inside class << self, b calls the class method a without a receiver.

  7. Where does a class method really live?

    Answer

    On the class's singleton class

    That is why A.singleton_class.instance_methods(false) lists the class methods.

  8. Which call hides a class method from outside callers?

    Answer

    private_class_method :x

    The visibility keywords act on the class the method is defined in, so a class method needs private_class_method.

  9. What does this program raise? class A class << self x = 1 def y = x end end p A.y

    Answer

    NameError, because x was a local variable and is gone

    class << self is not a scope for local variables, so x does not reach the method body.

  10. A class is full of self. methods that take all their state as arguments. What does the lesson call it?

    Answer

    A module in disguise

    Without class-level state those methods are functions, and a module expresses that better.

Article author: Arthur Isaev