diff options
author | John Mair <jrmair@gmail.com> | 2012-11-15 01:33:54 +0100 |
---|---|---|
committer | John Mair <jrmair@gmail.com> | 2012-11-15 01:33:54 +0100 |
commit | a2cc6265f1d073abac2e908b56fea6dff271e284 (patch) | |
tree | e8dae6efa06149aee094a96e16d1d953748daf99 | |
parent | 7a5d21ba746c49a0d3d344553e9c43bbb89cd57e (diff) | |
download | pry-a2cc6265f1d073abac2e908b56fea6dff271e284.tar.gz |
force precedence of modules over methods when using C::X syntax
-rw-r--r-- | lib/pry/helpers/command_helpers.rb | 16 | ||||
-rw-r--r-- | lib/pry/helpers/module_introspection_helpers.rb | 6 |
2 files changed, 18 insertions, 4 deletions
diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 614afcd9..08e724f6 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -23,6 +23,22 @@ class Pry end end + # Given a string and a binding, return the corresponding + # `Pry::Method` or `Pry::WrappedModule`. Also give precedence to modules + # when the `::` syntax is used. + # @param [String] input The full name of the method or module. + # @param [Binding] target The binding where the object is found. + # @return [Pry::WrappedModule, Pry::Method] The relevant code object. + def retrieve_code_object_from_string(input, target) + + # ensure modules have precedence when `MyClass::X` syntax is used. + if input =~ /::(?:\S+)\Z/ + Pry::WrappedModule.from_str(input,target) || Pry::Method.from_str(input, target) + else + Pry::Method.from_str(input,target) || Pry::WrappedModule.from_str(input, target) + end + end + # Return the file and line for a Binding. # @param [Binding] target The binding # @return [Array] The file and line diff --git a/lib/pry/helpers/module_introspection_helpers.rb b/lib/pry/helpers/module_introspection_helpers.rb index e57e4d76..77e31df9 100644 --- a/lib/pry/helpers/module_introspection_helpers.rb +++ b/lib/pry/helpers/module_introspection_helpers.rb @@ -27,10 +27,8 @@ class Pry elsif target.eval("defined? #{input} ") =~ /variable|constant/ && target.eval(input).respond_to?(:source_location) :sourcable_object - elsif Pry::Method.from_str(input,target) - :method - elsif Pry::WrappedModule.from_str(input, target) - :module + elsif co = retrieve_code_object_from_string(input, target) + co.is_a?(Pry::Method) ? :method : :module elsif target.eval("defined?(#{input})") =~ /variable|constant/ :variable_or_constant elsif find_command(input) |