Add ArgumentError to Object#in? if supplied param doesn't respond to :include? #268

Closed
wants to merge 2 commits into
from
@@ -5,7 +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)
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