提交 29a5aeaa 编写于 作者: D dmathieu

don't raise NoMethodError the tried method doesn't exists

上级 f674aed1
...@@ -28,6 +28,8 @@ class Object ...@@ -28,6 +28,8 @@ class Object
def try(*a, &b) def try(*a, &b)
if a.empty? && block_given? if a.empty? && block_given?
yield self yield self
elsif !a.empty? && !respond_to?(a.first)
nil
else else
__send__(*a, &b) __send__(*a, &b)
end end
......
...@@ -99,7 +99,7 @@ def setup ...@@ -99,7 +99,7 @@ def setup
def test_nonexisting_method def test_nonexisting_method
method = :undefined_method method = :undefined_method
assert !@string.respond_to?(method) assert !@string.respond_to?(method)
assert_raise(NoMethodError) { @string.try(method) } assert_nil @string.try(method)
end end
def test_valid_method def test_valid_method
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册