Class: Pry::Method::Disowned

Inherits:
Pry::Method show all
Defined in:
lib/pry/method/disowned.rb

Overview

A Disowned Method is one that’s been removed from the class on which it was defined.

e.g. class C

def foo
  C.send(:undefine_method, :foo)
  Pry::Method.from_binding(binding)
end

end

In this case we assume that the “owner” is the singleton class of the receiver.

This occurs mainly in Sinatra applications.

Constant Summary

Constants included from Helpers::DocumentationHelpers

Helpers::DocumentationHelpers::YARD_TAGS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pry::Method

#==, #alias?, #aliases, all_from_class, all_from_obj, #bound_method?, #comment, #doc, #dynamically_defined?, from_binding, from_class, from_obj, from_str, instance_method_definition?, instance_resolution_order, #is_a?, lookup_method_via_binding, method_definition?, #name_with_owner, #original_name, #pry_method?, #redefine, resolution_order, #respond_to?, #signature, singleton_class_of, singleton_class_resolution_order, #singleton_method?, singleton_method_definition?, #source, #source_file, #source_line, #source_range, #source_type, #super, #unbound_method?, #visibility, #wrapped, #wrapped_owner

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Forwardable

#def_private_delegators

Methods included from CodeObject::Helpers

#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

get_comment_content, process_comment_markup, process_rdoc, process_yardoc, process_yardoc_tag, strip_comments_from_c_code, strip_leading_whitespace

Constructor Details

#initialize(receiver, method_name) ⇒ Disowned

Create a new Disowned method.

Parameters:

  • receiver (Object)
  • method_name (String)


25
26
27
28
29
# File 'lib/pry/method/disowned.rb', line 25

def initialize(receiver, method_name)
  @receiver = receiver
  @name = method_name
  @method = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Raise a more useful error message instead of trying to forward to nil. rubocop:disable Style/MethodMissingSuper



52
53
54
55
56
57
58
59
# File 'lib/pry/method/disowned.rb', line 52

def method_missing(method_name, *args, &block)
  if method(:name).respond_to?(method_name)
    raise "Cannot call '#{method_name}' on an undef'd method."
  end

  method = Object.instance_method(:method_missing).bind(self)
  method.call(method_name, *args, &block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/pry/method/disowned.rb', line 19

def name
  @name
end

#receiverObject (readonly)

Returns the value of attribute receiver.



19
20
21
# File 'lib/pry/method/disowned.rb', line 19

def receiver
  @receiver
end

Instance Method Details

#ownerObject

Get the hypothesized owner of the method.

Returns:



46
47
48
# File 'lib/pry/method/disowned.rb', line 46

def owner
  class << receiver; self; end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

rubocop:enable Style/MethodMissingSuper

Returns:

  • (Boolean)


62
63
64
# File 'lib/pry/method/disowned.rb', line 62

def respond_to_missing?(method_name, include_private = false)
  !method(:name).respond_to?(method_name) || super
end

#source?Boolean

Can we get the source for this method?

Returns:

  • (Boolean)

    false



39
40
41
# File 'lib/pry/method/disowned.rb', line 39

def source?
  false
end

#undefined?Boolean

Is the method undefined? (aka ‘Disowned`)

Returns:

  • (Boolean)

    true



33
34
35
# File 'lib/pry/method/disowned.rb', line 33

def undefined?
  true
end