1
0
Fork 0
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:
Conrad Irwin 2011-10-25 13:37:41 -07:00
parent 27f6a7cd98
commit 67852d59d3
2 changed files with 17 additions and 3 deletions

View file

@ -94,7 +94,7 @@ class Pry
# @param [Object] obj
# @return [Array[Class, Module]]
def resolution_order(obj)
if obj.is_a?(Class)
if Class === obj
singleton_class_resolution_order(obj) + instance_resolution_order(Class)
else
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".
def all_from_common(obj, method_type)
%w(public protected private).map do |visibility|
obj.__send__(:"#{visibility}_#{method_type}s").map do |method_name|
new(obj.__send__(method_type, method_name), :visibility => visibility.to_sym)
safe_send(obj, :"#{visibility}_#{method_type}s").map do |method_name|
new(safe_send(obj, method_type, method_name), :visibility => visibility.to_sym)
end
end.flatten(1)
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
# the given class object, and any modules they include.
# If a module is included at multiple points in the ancestry, only

View file

@ -73,6 +73,12 @@ describe "ls" do
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/
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
describe "constants" do