diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index ef96df9227a5f..fe322b4865635 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* `Object#try` can't call private methods . *Vasiliy Ermolovich* + * `AS::Callbacks#run_callbacks` remove `key` argument. *Francesco Rodriguez* * `deep_dup` works more expectedly now and duplicates also values in +Hash+ instances and elements in +Array+ instances. *Alexey Gaziev* diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index e77a9da0ec20d..40a8101ca307f 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -1,5 +1,5 @@ class Object - # Invokes the method identified by the symbol +method+, passing it any arguments + # Invokes the public method identified by the symbol +method+, passing it any arguments # and/or the block specified, just like the regular Ruby Object#send does. # # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised @@ -29,7 +29,7 @@ def try(*a, &b) if a.empty? && block_given? yield self else - __send__(*a, &b) + public_send(*a, &b) end 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 b027fccab39d6..98ab82609e566 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -101,7 +101,7 @@ def test_nonexisting_method assert !@string.respond_to?(method) assert_raise(NoMethodError) { @string.try(method) } end - + def test_nonexisting_method_with_arguments method = :undefined_method assert !@string.respond_to?(method) @@ -138,4 +138,16 @@ def test_try_only_block_nil nil.try { ran = true } assert_equal false, ran end + + def test_try_with_private_method + klass = Class.new do + private + + def private_method + 'private method' + end + end + + assert_raise(NoMethodError) { klass.new.try(:private_method) } + end end