Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not fail when some garbage gets appanded to methods array

  • Loading branch information...
commit db70523e43d323e9e48c1662ab1ae02ee6b3401c 1 parent 2ad522d
@michaeldv authored
Showing with 15 additions and 7 deletions.
  1. +9 −7 lib/awesome_print/formatter.rb
  2. +6 −0 spec/methods_spec.rb
View
16 lib/awesome_print/formatter.rb
@@ -214,15 +214,17 @@ def awesome_instance(o)
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
6 spec/methods_spec.rb
@@ -445,4 +445,10 @@ def her
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
Please sign in to comment.
Something went wrong with that request. Please try again.