提交 cd233dd8 编写于 作者: J Jared McFarland 提交者: David Heinemeier Hansson

Only rescue a thrown NoMethodError, don't preemptively check for #include?; added tests

上级 2db9f8a4
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册