mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
make "ls" more robust to overridden methods.
This commit is contained in:
parent
27f6a7cd98
commit
67852d59d3
2 changed files with 17 additions and 3 deletions
|
@ -94,7 +94,7 @@ class Pry
|
||||||
# @param [Object] obj
|
# @param [Object] obj
|
||||||
# @return [Array[Class, Module]]
|
# @return [Array[Class, Module]]
|
||||||
def resolution_order(obj)
|
def resolution_order(obj)
|
||||||
if obj.is_a?(Class)
|
if Class === obj
|
||||||
singleton_class_resolution_order(obj) + instance_resolution_order(Class)
|
singleton_class_resolution_order(obj) + instance_resolution_order(Class)
|
||||||
else
|
else
|
||||||
klass = singleton_class(obj) rescue obj.class
|
klass = singleton_class(obj) rescue obj.class
|
||||||
|
@ -121,12 +121,20 @@ class Pry
|
||||||
# N.B. we pre-cache the visibility here to avoid O(N²) behaviour in "ls".
|
# N.B. we pre-cache the visibility here to avoid O(N²) behaviour in "ls".
|
||||||
def all_from_common(obj, method_type)
|
def all_from_common(obj, method_type)
|
||||||
%w(public protected private).map do |visibility|
|
%w(public protected private).map do |visibility|
|
||||||
obj.__send__(:"#{visibility}_#{method_type}s").map do |method_name|
|
safe_send(obj, :"#{visibility}_#{method_type}s").map do |method_name|
|
||||||
new(obj.__send__(method_type, method_name), :visibility => visibility.to_sym)
|
new(safe_send(obj, method_type, method_name), :visibility => visibility.to_sym)
|
||||||
end
|
end
|
||||||
end.flatten(1)
|
end.flatten(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Acts like send but ignores any methods defined below Object or Class in the
|
||||||
|
# inheritance heirarchy.
|
||||||
|
# This is required to introspect methods on objects like Net::HTTP::Get that
|
||||||
|
# have overridden the `method` method.
|
||||||
|
def safe_send(obj, method, *args, &block)
|
||||||
|
(Class === obj ? Class : Object).instance_method(method).bind(obj).call(*args, &block)
|
||||||
|
end
|
||||||
|
|
||||||
# Get the singleton classes of superclasses that could define methods on
|
# Get the singleton classes of superclasses that could define methods on
|
||||||
# the given class object, and any modules they include.
|
# the given class object, and any modules they include.
|
||||||
# If a module is included at multiple points in the ancestry, only
|
# If a module is included at multiple points in the ancestry, only
|
||||||
|
|
|
@ -73,6 +73,12 @@ describe "ls" do
|
||||||
mock_pry("ls -pM Class.new{ def goo; end; protected :goo }").should =~ /goo/
|
mock_pry("ls -pM Class.new{ def goo; end; protected :goo }").should =~ /goo/
|
||||||
mock_pry("ls -p Class.new{ def goo; end; private :goo }.new").should =~ /goo/
|
mock_pry("ls -p Class.new{ def goo; end; private :goo }.new").should =~ /goo/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should work for objects with an overridden method method" do
|
||||||
|
require 'net/http'
|
||||||
|
# This doesn't actually touch the network, promise!
|
||||||
|
mock_pry("ls Net::HTTP::Get.new('localhost')").should =~ /Net::HTTPGenericRequest methods/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "constants" do
|
describe "constants" do
|
||||||
|
|
Loading…
Reference in a new issue