liveBook · Manning

Private_instance_methods

As explained in The Well-Grounded Rubyist, Fourth Edition

private_instance_methods is a Ruby Module method that returns the private instance methods available from a class or module, optionally including inherited and included methods.

private_instance_methods is a Ruby Module method for inspecting the private instance methods available through a class or module. Called without an argument, it returns an array that includes methods defined there and methods inherited from ancestor classes or modules. section 5.4.2 section 15.2.3

Use the method-listing family to choose the visibility and scope you want to inspect:

  • Call MyClass.private_instance_methods to list private instance methods available from MyClass, including inherited and included methods. section 15.2.3
  • Pass false to restrict the result to private instance methods defined directly in the queried class or module. section 15.2.3
  • Compare private_instance_methods with public_instance_methods, protected_instance_methods, and instance_methods when investigating method visibility. section 7.7.3 section 15.2.3

Inspecting Kernel’s private toolkit

Run `$ ruby -e 'p Kernel.private_instance_methods.sort'` to print a sorted list of Kernel’s private instance methods. The result includes scripting and general-purpose operations such as require, load, and raise; because Object mixes in Kernel, these methods can be called without an explicit receiver. section 5.4.2

>> String.methods.grep(/methods/).sort
=> [:instance_methods, :methods, :private_instance_methods, 
:private_methods,:protected_instance_methods, :protected_methods,
 :public_instance_methods,:public_methods, :singleton_methods,
 :undefined_instance_methods]

Browse the related class- and object-level method-querying methods, including private_instance_methods, public_instance_methods, protected_instance_methods, and their object-level counterparts. section 15.2.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains visibility filtering and ancestor inclusion, but does not develop method lookup internals.
Examples
Uses Kernel.private_instance_methods.sort and the shell command ruby -e to inspect built-in private methods.
What it assumes
Assumes familiarity with Ruby classes, modules, instance methods, and method visibility while clarifying class-level inspection.
What you can do after
Diagnose private-method availability by querying a class or module and narrowing results with a false argument.
How it connects
Connects Module method introspection with private methods, public_instance_methods, protected_instance_methods, Kernel, and ancestor classes and modules.

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.