liveBook · Manning

Class method

A class method is a method associated with a class rather than a particular instance and invoked through a class or class-related object.

How 3 Manning books explain it

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.

How it is used
Assigns object creation, class-wide utilities, and collective operations to a class rather than an individual instance.
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 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.

Read how The Well-Grounded Rubyist, Fourth Edition explains class method →

Python How-To

A class method is a method associated with a class that receives the class as its first argument and can access class-level state and behavior.

How it is used
Uses class methods mainly as factory methods for creating instances from particular data forms.
How deep it goes
Defines @classmethod and cls, demonstrates constructor use, and stops short of deeper implementation internals.
Examples
Demonstrates Task construction from dictionaries and text, plus unittest checks for both class methods.
What you can do after
Build it from scratch by defining factory methods with @classmethod, cls, and constructor calls, then test them with unittest.

Read how Python How-To explains class method →

React Quickly, Second Edition

A class method is a function defined as part of a class that can access the class or instance context in which it is invoked.

How it is used
Simplifies JSX image rendering and organizes URL-building logic inside a class-based React component.
How deep it goes
Distinguishes pure utility methods from instance-dependent methods and compares their functional-component conversions.
Examples
Uses an image gallery, getImageSource, image URLs, this.props.index, and equivalent class-to-function implementations.
What you can do after
Apply class methods to simplify JSX and convert them into pure or local functions when building functional components.

Read how React Quickly, Second Edition explains class method →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.