Permalink
Browse files

Merge pull request #6280 from nashby/private-try

Object#try can't call private methods
  • Loading branch information...
2 parents 667d0bd + b8f394f commit 6f2aee3c18e15307fab6a73f9b15bd49e4f63bb0 @jonleighton jonleighton committed May 12, 2012
@@ -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*
@@ -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 <tt>Object#send</tt> 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
@@ -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

0 comments on commit 6f2aee3

Please sign in to comment.