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

Do not fail when some garbage gets appanded to methods array

This commit is contained in:
Mike Dvorkin 2011-12-05 13:49:13 -08:00
parent 2ad522d36e
commit db70523e43
2 changed files with 15 additions and 7 deletions

View file

@ -214,15 +214,17 @@ module AwesomePrint
def methods_array(a)
object = a.instance_variable_get('@__awesome_methods__')
tuples = a.map do |name|
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.
if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ]
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))
end
elsif object.respond_to?(:instance_method) # Is this an unbound method?
method_tuple(object.instance_method(name))
end
tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails.
end
width = (tuples.size - 1).to_s.size

View file

@ -445,4 +445,10 @@ describe "Methods arrays" do
hello = Hello.new
(hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }.should == [ :him, :his ]
end
it "appending garbage to methods array should not raise error" do
arr = 42.methods << [ :wtf ]
arr.ai(:plain => true).should_not raise_error(TypeError)
arr.ai(:plain => true).should =~ /\s+wtf\(\?\)\s+\?/
end
end