Skip to content

Commit

Permalink
Only rescue a thrown NoMethodError, don't preemptively check for #inc…
Browse files Browse the repository at this point in the history
…lude?; added tests
  • Loading branch information
jaredonline authored and dhh committed Apr 14, 2011
1 parent 2db9f8a commit cd233dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -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
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/object/inclusion_test.rb
Expand Up @@ -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.