Skip to content

Commit

Permalink
Object#try can't call private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed May 12, 2012
1 parent 2091e5a commit b8f394f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* `Object#try` can't call private methods . *Vasiliy Ermolovich*

This comment has been minimized.

Copy link
@fxn

fxn May 12, 2012

Member

Spurious space fixed here e5b91f2.


* `AS::Callbacks#run_callbacks` remove `key` argument. *Francesco Rodriguez* * `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* * `deep_dup` works more expectedly now and duplicates also values in +Hash+ instances and elements in +Array+ instances. *Alexey Gaziev*
Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/object/try.rb
@@ -1,5 +1,5 @@
class Object 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 <tt>Object#send</tt> does. # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
# #
# *Unlike* that method however, a +NoMethodError+ exception will *not* be raised # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
Expand Down Expand Up @@ -29,7 +29,7 @@ def try(*a, &b)
if a.empty? && block_given? if a.empty? && block_given?
yield self yield self
else else
__send__(*a, &b) public_send(*a, &b)
end end
end end
end end
Expand Down
14 changes: 13 additions & 1 deletion activesupport/test/core_ext/object_and_class_ext_test.rb
Expand Up @@ -101,7 +101,7 @@ def test_nonexisting_method
assert !@string.respond_to?(method) assert !@string.respond_to?(method)
assert_raise(NoMethodError) { @string.try(method) } assert_raise(NoMethodError) { @string.try(method) }
end end

def test_nonexisting_method_with_arguments def test_nonexisting_method_with_arguments
method = :undefined_method method = :undefined_method
assert !@string.respond_to?(method) assert !@string.respond_to?(method)
Expand Down Expand Up @@ -138,4 +138,16 @@ def test_try_only_block_nil
nil.try { ran = true } nil.try { ran = true }
assert_equal false, ran assert_equal false, ran
end 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 end

0 comments on commit b8f394f

Please sign in to comment.