Call singleton_methods on an object to list the names of its public and protected singleton methods. Private singleton methods are omitted. A singleton method belongs only to one object, or, when that object is a class, to the class and its subclasses. section 15.2.4
Use singleton_methods to inspect where object-specific behavior comes from:
- c.singleton_methods returns inherited singleton methods as well as methods defined directly on c's singleton class. section 15.2.4
- c.singleton_methods(false) restricts the result to singleton methods defined directly on c. section 15.2.4
- For a class, singleton_methods can reveal class methods inherited from ancestor classes; related class-level queries inspect visibility on the singleton class. section 7.7.3 section 15.2.4
Visibility filtering
class C end c = C.new class << c def x; end def y; end def z; end protected :y private :z end p c.singleton_methods.sort # [:x, :y] The private method z remains an instance method of c's singleton class, but singleton_methods does not report it.
