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.


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

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

Instance Attribute Details

#methodObject

Returns the value of attribute method


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

def method
  @method
end

#targetObject

Returns the value of attribute target


40
41
42
# File 'lib/pry/method/weird_method_locator.rb', line 40

def target
  @target
end

Class Method Details

.normal_method?(method, b) ⇒ 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)

28
29
30
31
32
# File 'lib/pry/method/weird_method_locator.rb', line 28

def normal_method?(method, b)
  method && (method.source_file && method.source_range rescue false) &&
  File.expand_path(method.source_file) == File.expand_path(b.eval('__FILE__')) &&
  method.source_range.include?(b.eval('__LINE__'))
end

.weird_method?(method, b) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/pry/method/weird_method_locator.rb', line 34

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

Instance Method Details

#get_methodPry::Method?

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

Returns:

  • (Pry::Method, nil)

    The Pry::Method that matches the given binding.


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

def get_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.


58
59
60
# File 'lib/pry/method/weird_method_locator.rb', line 58

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