Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1195 from dmathieu/try_undefined_method

Don't raise NoMethodError the tried method doesn't exists
  • Loading branch information...
commit 073f80eaa849c479fe547c3c34430db3c91702fb 2 parents f674aed + a85e00c
@dhh dhh authored
View
2  activesupport/lib/active_support/core_ext/object/try.rb
@@ -28,6 +28,8 @@ class Object
def try(*a, &b)
if a.empty? && block_given?
yield self
+ elsif !a.empty? && !respond_to?(a.first)
+ nil
else
__send__(*a, &b)
end
View
8 activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -99,7 +99,13 @@ def setup
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_raise(NoMethodError) { @string.try(method) }
+ assert_nil @string.try(method)
+ end
+
+ def test_nonexisting_method_with_arguments
+ method = :undefined_method
+ assert !@string.respond_to?(method)
+ assert_nil @string.try(method, 'llo', 'y')
end
def test_valid_method
Please sign in to comment.
Something went wrong with that request. Please try again.