Permalink
Browse files

Only rescue a thrown NoMethodError, don't preemptively check for #inc…

…lude?; added tests
  • Loading branch information...
1 parent 2db9f8a commit cd233dd87e6f0f9113a397531d37075cfa7c6526 @jaredonline jaredonline committed with dhh Apr 14, 2011
@@ -5,8 +5,11 @@ class Object
# characters = ["Konata", "Kagami", "Tsukasa"]
# "Konata".in?(characters) # => true
#
+ # This will throw an ArgumentError if the supplied argument doesnt not respond
+ # to +#include?+.
def in?(another_object)
- raise ArgumentError.new("You must supply another object that responds to include?") unless another_object.respond_to?(:include?)
another_object.include?(self)
+ rescue NoMethodError
+ raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
end
end
@@ -43,4 +43,8 @@ def test_in_module
assert A.in?(C)
assert !A.in?(A)
end
+
+ def test_no_method_catching
+ assert_raise(ArgumentError) { 1.in?(1) }
+ end
end

0 comments on commit cd233dd

Please sign in to comment.