1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #17361 from aripollak/try-bang-parity

Bring try! into parity with try.
This commit is contained in:
Godfrey Chan 2014-10-22 16:23:55 -07:00
commit 07b80c128f
2 changed files with 9 additions and 5 deletions

View file

@ -59,7 +59,7 @@ class Object
# does not implement the tried method.
def try!(*a, &b)
if a.empty? && block_given?
yield self
try(*a, &b)
else
public_send(*a, &b)
end

View file

@ -30,10 +30,6 @@ class ObjectTryTest < ActiveSupport::TestCase
assert_raise(NoMethodError) { @string.try!(method, 'llo', 'y') }
end
def test_try_only_block_bang
assert_equal @string.reverse, @string.try! { |s| s.reverse }
end
def test_valid_method
assert_equal 5, @string.try(:size)
end
@ -59,6 +55,10 @@ class ObjectTryTest < ActiveSupport::TestCase
assert_equal @string.reverse, @string.try { |s| s.reverse }
end
def test_try_only_block_bang
assert_equal @string.reverse, @string.try! { |s| s.reverse }
end
def test_try_only_block_nil
ran = false
nil.try { ran = true }
@ -69,6 +69,10 @@ class ObjectTryTest < ActiveSupport::TestCase
assert_equal @string.reverse, @string.try { reverse }
end
def test_try_with_instance_eval_block_bang
assert_equal @string.reverse, @string.try! { reverse }
end
def test_try_with_private_method_bang
klass = Class.new do
private