Class Methods in Ruby
Fast recall
Quick revision
Core answer
def self.method_name. Call it as User.find(...), without creating a User first. Use class methods for operations that belong to the class as a whole—such as factories or named constructors—not for behaviour that needs one instance's @state.Whether you know class methods are called on the class (not an instance) and defined with `def self.name`.
Trying to call a class method on an instance (or vice versa), or accessing @x from a class method (only @@x works).
Class methods = called on the class (Greeter.hello); defined with def self.name or class << self. For factories, utilities, counters. Can't see @x.
Interview-ready guide
Interview answer
A class method is called on the class object rather than on one instance. Define one with def self.from_cents(value) and call it as Money.from_cents(250). Inside that method, self is the Money class object. An instance method such as money.format instead receives one Money object as self.
A factory or named constructor is a common class-method use. Temperature.from_fahrenheit(68) accepts a convenient external value, converts it, and calls new to return a normal Temperature object. The instance method freezing? then works with that object's @celsius value.
An instance variable used by a class method, such as @created, belongs to the class object. It is not the same storage as @created inside an instance. It is also different from the class variable @@created. Subclasses inherit class methods, but they do not automatically share the parent's class-instance-variable value.
Use a class method when the natural receiver is the type itself, such as a factory, parser, or lookup. Use an instance method when the operation needs one object's state. A generic calculation that belongs to neither may fit a separate service or module better. Avoid turning class methods and mutable class state into hidden global dependencies.
Class Methods in Ruby
Direct Answer
A Ruby class method is a singleton method whose receiver is the class object, commonly defined with def self.method_name. Call it as User.find(...), without creating a User first. Use class methods for operations that belong to the class as a whole—such as factories or named constructors—not for behaviour that needs one instance's @state.
Deep Dive & Architecture
Ruby classes are instances of Class. This means Report.generate follows the same message-send model as report.generate; the difference is the receiver. def self.generate adds the method to that one class object's singleton class. Opening class << self exposes the same place for several definitions.
State follows the receiver too. @count used by a class method is stored on the class object. @count used by an instance method is stored on that instance. They share a spelling but not a storage location. A subclass is a different class object, so it does not automatically receive the parent's class-instance-variable value even though class methods themselves may be inherited.
Factories are a strong use case because they keep alternate construction rules next to the type. .from_json, .parse, or .empty can validate and translate input before returning a normal instance. The ordinary .new path remains available when callers already have constructor-ready values.
Class methods are not a licence to turn a class into global state. Mutable configuration and counters on a widely used class create hidden coupling and concurrency concerns. Pass dependencies explicitly or use a dedicated object when the operation has its own lifecycle.
Code Example
class Temperature
attr_reader :celsius
def initialize(celsius)
@celsius = celsius
end
def self.from_fahrenheit(value)
new((value - 32) * 5.0 / 9)
end
def freezing?
@celsius <= 0
end
end
temperature = Temperature.from_fahrenheit(32)
temperature.celsius # => 0.0
temperature.freezing? # => true
Common Mistakes & Pitfalls
Trying to call a class method on an instance (or vice versa), or accessing @x from a class method (only @@x works).
Check your understanding
Answer these without looking back at the article.
- When would you use Class Methods in a real project?
- How would you compare Class Methods with an alternative?
- What is a common mistake when using Class Methods?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.