force precedence of modules over methods when using C::X syntax

This commit is contained in:
John Mair 2012-11-15 01:33:54 +01:00
parent 7a5d21ba74
commit a2cc6265f1
2 changed files with 18 additions and 4 deletions

View File

@ -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

View File

@ -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)