liveBook · Manning

Methods

As explained in The Well-Grounded Rubyist, Fourth Edition

methods is a Ruby method that returns an array containing the names of methods available to an object.

Ruby’s methods method returns an array of symbols naming the methods an object can call. Sorting the result makes a large capability list easier to browse. section 2.3 section 15.2.1

A methods query includes:

  • Methods inherited through the object’s ancestry. section 7.7.1
  • Methods supplied by included modules, even when inclusion happens after an object exists. section 7.7.1
  • Singleton methods defined specifically for that object. section 7.7.1
  • For class and module objects, methods callable on the class or module itself—not methods available to their instances. section 7.7.1

Filtering a capability list

Given string = "Test string", string.methods.grep(/case/).sort finds case-related names such as :downcase, :swapcase, and :upcase. The returned symbols can be filtered, sorted, and compared to answer questions about an object’s capabilities. section 15.2.1 section 15.2

>> "I am a String object".methods.sort

Inspecting and sorting a real String object’s method names makes the returned array and its scale visible. section 7.7.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains returned symbols, sorting, filtering, singleton methods, inheritance, and modules, but defers broader method lookup details.
Examples
Demonstrates sorted object listings, String methods, singleton methods, module inclusion, case filtering, and bang-method comparisons.
What it assumes
Assumes familiarity with Ruby objects, messages, arrays, symbols, sorting, filtering, inheritance, and modules.
What you can do after
Evaluate an object’s capabilities, filter its method names, and investigate methods supplied by inheritance, modules, or singleton definitions.
How it connects
Connects methods with objects, classes, modules, singleton methods, inheritance, method lookup, symbols, and capability queries.

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.