liveBook · Manning

Singleton method

As explained in The Well-Grounded Rubyist, Fourth Edition

A singleton method is a method defined on one particular Ruby object rather than generally on all instances of its class.

obj = Object.new
def obj.talk
  puts "Hi!"
end
obj.talk  #1

Define a method with a particular object as the receiver, then call it on that object. The method belongs to that object rather than to every instance of its class. section 13.1

Contrast `def obj.talk` with `def talk` inside a class: the first defines a singleton method for `obj`; the second defines an instance method available to instances of that class. The receiving object can call either kind without needing to distinguish them. section 3.1.1

A singleton method is stored as an instance method of its object’s singleton class. Method lookup can therefore find behavior supplied by the object’s ordinary class and ancestors, as well as behavior specific to that object. section 13.1.1

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

A class is also an object. Defining `Ticket.most_expensive` adds a singleton method to that class object, so callers send the message to `Ticket`, not to an individual ticket. Such a definition is commonly called a class method. section 3.6.3

“Class method” can also mean any method callable directly on a class object, whether or not it is that object’s singleton method. Unlike a singleton method on an ordinary object, one defined on a class object can also be called on its subclasses: their singleton classes participate in an inheritance relationship. section 13.1.4

To inspect an object, `methods` lists callable methods without showing where they were defined; `singleton_methods` narrows the list to singleton methods. It includes public and protected methods, but not private ones. Passing `false` excludes inherited singleton methods when querying a class. section 7.7.1 section 15.2.4

Use per-object additions selectively. Class methods are their common use; other uses include adding accessors when database column names become known at runtime. Integer, float, and symbol objects cannot have singleton methods added to them. section 3.8 section 13.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains definition, singleton-class storage, class-method inheritance, and introspection, including visibility and object-type exceptions.
Examples
Uses a talking object, a highest-priced-ticket class method, and method listings on a string.
What it assumes
Assumes familiarity with Ruby objects, classes, and method calls; explains how instance methods differ from per-object definitions.
What you can do after
Build it from scratch on an individual object and diagnose its available methods with `singleton_methods`.
How it connects
Connects singleton methods to instance methods, singleton classes, class methods, inheritance, and method lookup.

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.