Method: ObjectShadow::MethodIntrospection#methods

Defined in:
lib/object_shadow/method_introspection.rb

#methods(target: :self, scope: :public, inherit: :exclude_class) ⇒ Object

#shadow#methods returns a sorted list of methods related to the object in question. It lets you specify the kind of methods you want to retrieve by the following keyword parameters:

target: (default :self)

  • :self - This returns the list of methods available to call

    on the current object, including singleton methods
    If the object is a class/module, this means that it
    will return class methods
    
  • :class - This will refer to the object’s class (via the ‘class`) method

    and return its methods, usually to be called from an instance
    If called for a class or module, it will return `Class`' methods
    
  • :instances - This will list all methods which instances of the class (or the class

    that includes the module, in case of a module) in question will have
    Raises an ArgumentError when called on with a non-`Module`
    

scope: (default :public)

  • :public - Restrict to to methods of public visibility

  • :protected - Restrict to to methods of protected visibility

  • :private - Restrict to to methods of private visibility

  • :all - Restrict to to methods of public visibility

inherit: (default :exclude_object)

  • :singleton - Show only methods directly defined in the object’s singleton class

  • :self - Show singleton methods and methods directly defined in the object’s class,

    but do not traverse the inheritance chain
    
  • :exclude_class - Stop inheritance chain just before Class or Module. For

    non-modules it fallbacks to :exclude_object
    
  • :exclude_object - Stop inheritance chain just before Object

  • :all - Show methods from the whole inheritance chain



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/object_shadow/method_introspection.rb', line 50

def methods(target: :self, scope: :public, inherit: :exclude_class)
  MethodIntrospection.lookup_chain_for(object, target, inherit, true).flat_map { |lookup_class|
    full_inheritance_lookup = inherit == :all || inherit == true

    case scope
    when :public
      lookup_class.public_instance_methods(full_inheritance_lookup)
    when :protected
      lookup_class.protected_instance_methods(full_inheritance_lookup)
    when :private
      lookup_class.private_instance_methods(full_inheritance_lookup)
    when :all
      lookup_class.instance_methods(full_inheritance_lookup) +
      lookup_class.private_instance_methods(full_inheritance_lookup)
    else
      Kernel.raise ArgumentError, \
        "(ObjectShadow) Method scope: must be one of [:public, :protected, :private, :all]"
    end
  }.uniq.sort
end