liveBook · Manning

Class method

As explained in The Well-Grounded Rubyist, Fourth Edition

A class method is a method callable directly on a Ruby class object, including methods defined on that object or inherited by its class.

Suppose several tickets have prices and you want the highest-priced one. `Ticket.most_expensive(th, cc, fg)` sends the request to `Ticket`, not to any ticket in the list. A class method is, broadly, a method callable directly on a Ruby class object. `most_expensive` is defined on that particular class object; calling `fg.most_expensive` fails because `fg` is a different object. section 3.6.3 section 3.6.5

def Ticket.most_expensive(*tickets)
  tickets.max_by(&:price)
end

This definition attaches `most_expensive` directly to `Ticket`. Its arguments are the ticket instances to compare. section 3.6.3

Put an operation on the class when no individual instance can sensibly perform it. `Ticket.new` creates an instance before there is one to receive messages; `File.open` initiates file access and returns a `File` object. Finding the most expensive ticket is different: it examines existing instances, but the comparison concerns tickets collectively rather than one ticket’s own state. Class methods can also group utilities such as `Temperature.c2f` and `Temperature.f2c` without requiring temperature instances. section 3.6.4

Keep the receiver in view:

  • `Ticket#price` denotes an instance method; `Ticket.most_expensive` denotes a class method. `Ticket::most_expensive` is another notation for the latter. section 3.6.5
  • Not every method callable on a class was defined specifically for that class. Classes inherit methods through `Class` and its ancestors: `String.ancestors` is relevant to classes, while `String.object_id` uses a method available to every object. section 3.6.3
  • A class object has its own state, separate from its instances. A class method can nevertheless work with data shared by class and instance methods: `Car.add_make` registers makes, `Car.total_count` reports the total, and `Car#make_mates` reports a count for one car’s make. section 3.6.5 section 5.2.5

The usual direct-definition form is `def Ticket.most_expensive`; inside a class definition, `def self.total_count` uses `self` for the class object. Such definitions are singleton methods on a class object, but “class method” is broader than “singleton method.” A class method defined this way can also be called on a subclass: Ruby searches the subclass’s singleton class and then the superclass’s singleton class. Ordinary singleton methods on other objects do not gain receivers this way. section 3.6.3 section 5.2.5 section 13.1.4

Adding a class method through a mix-in

Including a module normally makes its instance methods available to instances, not to the class object. A module’s `included` callback can also define a method on the including class, so `C.a_class_method` works alongside an instance method supplied by the same module. section 15.1.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains direct definitions, inherited callable methods, receiver distinctions, subclass lookup through singleton classes, and addition through an inclusion callback.
Examples
Uses ticket-price comparison, temperature conversion, car statistics, subclass calls, and a module callback.
What it assumes
Assumes familiarity with classes, instances, and method calls; develops the singleton-class lookup explanation separately.
What you can do after
Build it from scratch by defining a method on a class object and choosing that receiver when an instance is inappropriate.
How it connects
Connects class methods to singleton methods, instance methods, class objects, inheritance, class variables, and module inclusion.

Also explained in

Compare how 3 books explain class method →

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.