From a2cc6265f1d073abac2e908b56fea6dff271e284 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 15 Nov 2012 01:33:54 +0100 Subject: [PATCH] force precedence of modules over methods when using C::X syntax --- lib/pry/helpers/command_helpers.rb | 16 ++++++++++++++++ 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)