1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Correctly format methods array when object#method is overridden

This commit is contained in:
Mike Dvorkin 2011-05-13 13:33:16 -07:00
parent 62c07edea2
commit b9cf4a4e0a
2 changed files with 18 additions and 5 deletions

View file

@ -180,13 +180,15 @@ class AwesomePrint
def methods_array(a)
object = a.instance_variable_get('@__awesome_methods__')
tuples = a.map do |name|
if object.respond_to?(name, true) # Regular method?
method_tuple(object.method(name))
elsif object.respond_to?(:instance_method) # Unbound method?
tuple = if object.respond_to?(name, true) # Is this a regular method?
the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden.
if the_method && the_method.respond_to?(:arity) # Is this original object#method?
method_tuple(the_method) # Yes, we are good.
end
elsif object.respond_to?(:instance_method) # Is this an unbound method?
method_tuple(object.instance_method(name))
else # WTF method.
[ name.to_s, '(?)', '' ]
end
tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
end
width = (tuples.size - 1).to_s.size

View file

@ -559,5 +559,16 @@ EOS
my = My.new
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
end
it "should handle a class defines its own #method method (ex. request.method)" do
class My
def method
'POST'
end
end
my = My.new
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
end
end
end