Class: Pry::Method::WeirdMethodLocator
- 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:
- The Pry::Method is from a subclass
- 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
-
#method ⇒ Object
Returns the value of attribute method.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.normal_method?(method, binding) ⇒ Boolean
Whether the given method object matches the associated binding.
- .weird_method?(method, binding) ⇒ Boolean
Instance Method Summary collapse
- #all_methods_for(obj) ⇒ Object private
- #expanded_source_location(source_location) ⇒ Object private
-
#find_method ⇒ Pry::Method?
The Pry::Method that matches the given binding.
-
#find_method_in_superclass ⇒ Pry::Method?
private
it's possible in some cases that the method we find by this approach is a sub-method of the one we're currently in, consider:.
-
#find_renamed_method ⇒ Pry::Method?
private
This is the case where the name of a method has changed (via alias_method) so we locate the Method object for the renamed method.
- #index_to_line_number(index) ⇒ Object private
-
#initialize(method, target) ⇒ WeirdMethodLocator
constructor
A new instance of WeirdMethodLocator.
- #lines_for_file(file) ⇒ Object private
-
#lost_method? ⇒ Boolean
Whether the Pry::Method is unrecoverable This usually happens when the method captured by the Binding has been subsequently deleted.
- #normal_method?(method) ⇒ Boolean private
- #pry_file? ⇒ Boolean private
-
#renamed_method_source_location ⇒ Array<String, Fixnum>
private
Use static analysis to locate the start of the method definition.
- #skip_superclass_search? ⇒ Boolean private
- #target_file ⇒ Object private
- #target_line ⇒ Object private
- #target_self ⇒ Object private
- #valid_file?(file) ⇒ Boolean private
Constructor Details
#initialize(method, target) ⇒ WeirdMethodLocator
Returns a new instance of WeirdMethodLocator.
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
#method ⇒ Object
Returns the value of attribute method.
49 50 51 |
# File 'lib/pry/method/weird_method_locator.rb', line 49 def method @method end |
#target ⇒ Object
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.
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.(method.source_file) == File.(binding_file)) && method.source_range.include?(binding_line) end rescue StandardError false end |
.weird_method?(method, binding) ⇒ 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
#all_methods_for(obj) ⇒ Object (private)
215 216 217 218 219 |
# File 'lib/pry/method/weird_method_locator.rb', line 215 def all_methods_for(obj) obj.public_methods(false) + obj.private_methods(false) + obj.protected_methods(false) end |
#expanded_source_location(source_location) ⇒ Object (private)
167 168 169 170 171 172 173 174 175 |
# File 'lib/pry/method/weird_method_locator.rb', line 167 def (source_location) return unless source_location if pry_file? source_location else [File.(source_location.first), source_location.last] end end |
#find_method ⇒ Pry::Method?
Returns 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 |
#find_method_in_superclass ⇒ Pry::Method? (private)
it's possible in some cases that the method we find by this approach is a sub-method of the one we're currently in, consider:
class A; def b; binding.pry; end; end class B < A; def b; super; end; end
Given that we can normally find the source_range of methods, and that we know which FILE and LINE the binding is at, we can hope to disambiguate these cases.
This obviously won't work if the source is unavaiable for some reason, or if both methods have the same FILE and LINE.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pry/method/weird_method_locator.rb', line 131 def find_method_in_superclass guess = method return guess if skip_superclass_search? while guess # needs rescue if this is a Disowned method or a C method or something... # TODO: Fix up the exception handling so we don't need a bare rescue return guess if normal_method?(guess) break if guess == guess.super guess = guess.super end # Uhoh... none of the methods in the chain had the right `__FILE__` and # `__LINE__` due to unknown circumstances. # TODO: we should warn the user when this happens. nil end |
#find_renamed_method ⇒ Pry::Method? (private)
This is the case where the name of a method has changed (via alias_method) so we locate the Method object for the renamed method.
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pry/method/weird_method_locator.rb', line 156 def find_renamed_method return unless valid_file?(target_file) alias_name = all_methods_for(target_self).find do |v| location = target_self.method(v).source_location location && (location) == renamed_method_source_location end alias_name && Pry::Method(target_self.method(alias_name)) end |
#index_to_line_number(index) ⇒ Object (private)
197 198 199 200 |
# File 'lib/pry/method/weird_method_locator.rb', line 197 def index_to_line_number(index) # Pry.line_buffer is 0-indexed pry_file? ? index : index + 1 end |
#lines_for_file(file) ⇒ Object (private)
206 207 208 209 210 211 212 213 |
# File 'lib/pry/method/weird_method_locator.rb', line 206 def lines_for_file(file) @lines_for_file ||= {} @lines_for_file[file] ||= if Pry.eval_path == file Pry.line_buffer else File.readlines(file) end 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.
69 70 71 |
# File 'lib/pry/method/weird_method_locator.rb', line 69 def lost_method? !!(find_method.nil? && renamed_method_source_location) end |
#normal_method?(method) ⇒ Boolean (private)
80 81 82 |
# File 'lib/pry/method/weird_method_locator.rb', line 80 def normal_method?(method) self.class.normal_method?(method, target) end |
#pry_file? ⇒ Boolean (private)
106 107 108 109 110 111 112 113 114 |
# File 'lib/pry/method/weird_method_locator.rb', line 106 def pry_file? file = if target.respond_to?(:source_location) target.source_location.first else target.eval('__FILE__') end Pry.eval_path == file end |
#renamed_method_source_location ⇒ Array<String, Fixnum> (private)
Use static analysis to locate the start of the method definition.
We have the __FILE__
and __LINE__
from the binding and the
original name of the method so we search up until we find a
def/define_method, etc defining a method of the appropriate name.
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/pry/method/weird_method_locator.rb', line 184 def renamed_method_source_location if defined?(@original_method_source_location) return @original_method_source_location end source_index = lines_for_file(target_file)[0..(target_line - 1)].rindex do |v| Pry::Method.method_definition?(method.name, v) end @original_method_source_location = source_index && [target_file, index_to_line_number(source_index)] end |
#skip_superclass_search? ⇒ Boolean (private)
75 76 77 78 |
# File 'lib/pry/method/weird_method_locator.rb', line 75 def skip_superclass_search? target_mod = @target.eval('self').class target_mod.ancestors.take_while { |mod| mod != target_mod }.any? end |
#target_file ⇒ Object (private)
88 89 90 91 92 93 94 95 96 |
# File 'lib/pry/method/weird_method_locator.rb', line 88 def target_file file = if target.respond_to?(:source_location) target.source_location.first else target.eval('__FILE__') end pry_file? ? file : File.(file) end |
#target_line ⇒ Object (private)
98 99 100 101 102 103 104 |
# File 'lib/pry/method/weird_method_locator.rb', line 98 def target_line if target.respond_to?(:source_location) target.source_location.last else target.eval('__LINE__') end end |
#target_self ⇒ Object (private)
84 85 86 |
# File 'lib/pry/method/weird_method_locator.rb', line 84 def target_self target.eval('self') end |