mirror of
https://github.com/github/rails.git
synced 2026-01-11 07:38:05 -05:00
don't raise NoMethodError the tried method doesn't exists
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -99,7 +99,7 @@ class ObjectTryTest < Test::Unit::TestCase
|
||||
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_valid_method
|
||||
|
||||
Reference in New Issue
Block a user