liveBook · Manning

Protected_instance_methods

As explained in The Well-Grounded Rubyist, Fourth Edition

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

protected_instance_methods is a Module method for inspecting protected instance methods on a class or module. Unlike obj.protected_methods, which examines a regular object's available methods, it queries the instance-method definitions associated with the class or module itself. section 7.7.3 section 15.2.3

Choose the scope of the method list with an optional argument:

  • protected_instance_methods returns protected instance methods from the receiver and, by default, its ancestor classes and modules. section 15.2.3
  • protected_instance_methods(false) restricts the result to methods defined directly by the queried class or module. section 15.2.3
  • Related queries include instance_methods for public and protected methods, public_instance_methods for public methods, and private_instance_methods for private methods. section 15.2.3
  • Keeping the receiver and requested scope clear prevents confusion between a class or module query and an object-level query. section 7.7.3

Inspecting only a class's protected definitions

Use the same receiver-and-scope pattern shown by other class-level queries: Range.protected_instance_methods(false) asks for protected instance methods defined by Range itself, while Range.protected_instance_methods also includes definitions supplied by ancestors and included modules. The false form is useful when narrowing a method list to the class's own definitions. section 15.2.3 section 7.7.3

>> Range.instance_methods(false).sort
=>  [:%, :==, :===, :begin, :bsearch, :count, :cover?, :each, :end,
 :entries, :eql?, :exclude_end?, :first, :hash, :include?, :inspect,
 :last, :max, :member?, :min, :minmax, :overlap?, :pretty_print,
 :reverse_each, :size, :step, :to_a, :to_s]

Compare Range.instance_methods(false) with the corresponding protected-instance query to see how the false argument narrows inspection to definitions made directly by the receiver. section 15.2.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains receiver scope and inheritance filtering, but does not develop protected-method dispatch internals.
Examples
Uses String and Range method-list queries, including false-scoped listings and comparisons of method-name lists.
What it assumes
Assumes familiarity with Ruby classes, modules, instance methods, visibility, and ancestor relationships.
What you can do after
Diagnose where protected instance methods come from by comparing receiver-only and ancestor-inclusive method lists.
How it connects
Connects Module introspection with instance_methods, public_instance_methods, private_instance_methods, inheritance, and included 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.