self, Class Methods & Class Variables

In Ruby, self is a keyword that points at the current object, and it is the key to understanding class methods, the singleton class, and how class-level state is stored.

Learn self, Class Methods & Class Variables in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll learn what self means in each context, how to write class methods with def self.method and , and why @@class variables are a trap you can avoid.

What You'll Learn in This Lesson

1️⃣ What Is self?

There is always a current object in Ruby, and self names it. At the top level it's the special main object; inside a class body it's the class; inside an instance method it's the object the method was called on. Reading self in each spot tells you exactly which object owns the code running there.

2️⃣ Class Methods: def self.method and class << self

A class method is called on the class itself, not on an instance — like Time.now or Array.new . The most common way to write one is def self.name . These make great factory methods that build and return a configured object.

When a class has several class methods, repeating def self. gets noisy. Open the singleton class once with and define them all inside, just like ordinary methods.

3️⃣ Class Variables (@@) vs Class Instance Variables

A @@variable stores state at the class level and is shared by the class and all of its subclasses through one slot. That sharing is occasionally what you want — but it bites hard in inheritance, because a subclass assigning to the same name overwrites the parent's value. A class instance variable (a plain @var written in class scope) gives each class its own private copy instead.

🎯 Your Turn

Fill in the two blanks so User.total reports how many users were created. The shared @@total counter is appropriate here because there are no subclasses to surprise.

Combine everything: a class method factory that also tracks how many objects it has built — using a class instance variable so subclasses wouldn't interfere. Run with ruby book.rb .

📋 Quick Reference — self & Class-Level Code

Practice quiz

What does self refer to inside an instance method?

  • The class itself
  • The main object
  • The object the method was called on
  • nil

Answer: The object the method was called on. Inside an instance method, self is the particular instance the method was called on.

What does self refer to inside a class body (not in a method)?

  • The class itself
  • The instance
  • The superclass
  • main

Answer: The class itself. Inside a class body, self is the class itself — which is why def self.foo defines a class method.

How do you define a class method called build?

  • def build
  • def @build
  • class build
  • def self.build

Answer: def self.build. def self.build defines a method on the class object, i.e. a class method.

What is class << self used for?

  • Inheriting from self
  • Opening the singleton class to define several class methods
  • Freezing the class
  • Creating an instance

Answer: Opening the singleton class to define several class methods. class << self opens the singleton class so you can group many class methods and class-level accessors.

A @@class variable is shared by...

  • The class and all of its subclasses
  • Only the class
  • Only instances
  • Nothing — each instance gets its own

Answer: The class and all of its subclasses. A @@variable is shared by the class and every subclass through one storage slot — the famous pitfall.

Given class Base with @@r = "base", then class Sub < Base sets @@r = "sub". What does Base.r return?

  • "base"
  • nil
  • "sub"
  • an error

Answer: "sub". The subclass overwrites the SAME @@r, so Base.r returns "sub" — the pitfall in action.

What is the safer alternative to @@class variables for per-class state?

  • Global variables
  • Class instance variables (@var in class scope)
  • Local variables
  • Constants only

Answer: Class instance variables (@var in class scope). A class instance variable (a plain @var written in class scope) gives each class its own independent copy.

How do you call a class method named from_celsius on the Temperature class?

  • Temperature.new.from_celsius(100)
  • from_celsius(100)
  • Temperature::new(100)
  • Temperature.from_celsius(100)

Answer: Temperature.from_celsius(100). Class methods are called on the class itself: Temperature.from_celsius(100).

If you write def from_celsius(c) (without self) inside a class, what do you get?

  • A class method
  • An instance method
  • A syntax error
  • A constant

Answer: An instance method. Without self, def from_celsius defines an INSTANCE method by mistake.

What does self refer to at the top level of a Ruby script?

  • nil
  • Object
  • The special main object
  • The first class defined

Answer: The special main object. At the top level, self is a special object called main.