Class: Pry::Method::WeirdMethodLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/pry/method/weird_method_locator.rb

Overview

This class is responsible for locating the real ‘Pry::Method` object captured by a binding.

Given a ‘Binding` from inside a method and a ’seed’ Pry::Method object, there are primarily two situations where the seed method doesn’t match the Binding:

  1. The Pry::Method is from a subclass

  2. The Pry::Method represents a method of the same name while the original

was renamed to something else. For 1. we search vertically up the inheritance chain, and for 2. we search laterally along the object’s method table.

When we locate the method that matches the Binding we wrap it in Pry::Method and return it, or return nil if we fail.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, target) ⇒ WeirdMethodLocator

Returns a new instance of WeirdMethodLocator.

Parameters:

  • method (Pry::Method)

    The seed method.

  • target (Binding)

    The Binding that captures the method we want to locate.



55
56
57
58
# File 'lib/pry/method/weird_method_locator.rb', line 55

def initialize(method, target)
  @method = method
  @target = target
end

Instance Attribute Details

#methodObject

Returns the value of attribute method.



49
50
51
# File 'lib/pry/method/weird_method_locator.rb', line 49

def method
  @method
end

#targetObject

Returns the value of attribute target.



50
51
52
# File 'lib/pry/method/weird_method_locator.rb', line 50

def target
  @target
end

Class Method Details

.normal_method?(method, binding) ⇒ Boolean

Whether the given method object matches the associated binding. If the method object does not match the binding, then it’s most likely not the method captured by the binding, and we must commence a search.

Parameters:

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pry/method/weird_method_locator.rb', line 29

def normal_method?(method, binding)
  if method && method.source_file && method.source_range
    if binding.respond_to?(:source_location)
      binding_file, binding_line = binding.source_location
    else
      binding_file = binding.eval('__FILE__')
      binding_line = binding.eval('__LINE__')
    end
    (File.expand_path(method.source_file) == File.expand_path(binding_file)) &&
      method.source_range.include?(binding_line)
  end
rescue StandardError
  false
end

.weird_method?(method, binding) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/pry/method/weird_method_locator.rb', line 44

def weird_method?(method, binding)
  !normal_method?(method, binding)
end

Instance Method Details

#find_methodPry::Method?

Returns The Pry::Method that matches the given binding.

Returns:

  • (Pry::Method, nil)

    The Pry::Method that matches the given binding.



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

def find_method
  find_method_in_superclass || find_renamed_method
end

#lost_method?Boolean

Returns Whether the Pry::Method is unrecoverable This usually happens when the method captured by the Binding has been subsequently deleted.

Returns:

  • (Boolean)

    Whether the Pry::Method is unrecoverable This usually happens when the method captured by the Binding has been subsequently deleted.



69
70
71
# File 'lib/pry/method/weird_method_locator.rb', line 69

def lost_method?
  !!(find_method.nil? && renamed_method_source_location)
end