diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-01-13 13:28:14 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-01-13 13:28:28 -0800 |
commit | 0932b012fae2ff21214f610a0fd78a55ae4e2420 (patch) | |
tree | 82b521a5ddf17c3bc4fd7762c95d49f29d31de15 /activesupport | |
parent | 6fe739ad752aeeaaf274ced6111685d2d6ed2eb8 (diff) | |
parent | 5a43908c7414996354ca427354d98d789e0210e7 (diff) | |
download | rails-0932b012fae2ff21214f610a0fd78a55ae4e2420.tar.gz rails-0932b012fae2ff21214f610a0fd78a55ae4e2420.tar.bz2 rails-0932b012fae2ff21214f610a0fd78a55ae4e2420.zip |
Sync 'rails/rails/master'
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/misc.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/object_and_class_ext_test.rb | 19 |
2 files changed, 11 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 4570570bbc..c0a109ecf3 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -102,6 +102,6 @@ class Object # Person.try(:find, 1) # @people.try(:map) {|p| p.name} def try(method, *args, &block) - send(method, *args, &block) if respond_to?(method, true) + send(method, *args, &block) unless self.nil? end end diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 2f79b6f67f..0bdbd14f33 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -256,21 +256,13 @@ class ObjectTryTest < Test::Unit::TestCase def test_nonexisting_method method = :undefined_method assert !@string.respond_to?(method) - assert_nil @string.try(method) + assert_raises(NoMethodError) { @string.try(method) } end def test_valid_method assert_equal 5, @string.try(:size) end - def test_valid_private_method - class << @string - private :size - end - - assert_equal 5, @string.try(:size) - end - def test_argument_forwarding assert_equal 'Hey', @string.try(:sub, 'llo', 'y') end @@ -278,4 +270,13 @@ class ObjectTryTest < Test::Unit::TestCase def test_block_forwarding assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' } end + + def test_nil_to_type + assert_nil nil.try(:to_s) + assert_nil nil.try(:to_i) + end + + def test_false_try + assert_equal 'false', false.try(:to_s) + end end |