Skip to content
Browse files

Change Object#try to raise NoMethodError on private methods and alway…

…s return nil when Object is nil [Pratik Naik, Lawrence Pit]
  • Loading branch information...
1 parent 296ca4d commit 5339f813be99012aba01586743d8b24f065e7034 @lifo lifo committed
3 actionpack/lib/action_controller/test_process.rb
@@ -484,7 +484,8 @@ def method_missing(selector, *args, &block)
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
def fixture_file_upload(path, mime_type = nil, binary = false)
-"#{ActionController::TestCase.try(:fixture_path)}#{path}", mime_type, binary)
+ fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
+"#{fixture_path}#{path}", mime_type, binary)
# A helper to make it easier to test different route configurations.
2 activesupport/lib/active_support/core_ext/object/misc.rb
@@ -102,6 +102,6 @@ def acts_like?(duck)
# Person.try(:find, 1)
# @people.try(:map) {|p|}
def try(method, *args, &block)
- send(method, *args, &block) if respond_to?(method, true)
+ send(method, *args, &block) unless self.nil?
19 activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -256,21 +256,13 @@ def setup
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_nil @string.try(method)
+ assert_raises(NoMethodError) { @string.try(method) }
def test_valid_method
assert_equal 5, @string.try(:size)
- 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')
@@ -278,4 +270,13 @@ def test_argument_forwarding
def test_block_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
+ 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

7 comments on commit 5339f81


Hmm… so .try becomes the same as .andand?

Before it was subtly different, although I can’t think of a reason to use it as it was.


this change was probably “inspired” by this post:


Great! So much more useful and reliable.

foo.try(:whatever, clever)

yeah, that’s much more readable than

foo && foo.whatever(clever)

s/foo/big.long.expression/ and it makes sense



(tiny = big.long.expression) && tiny.whatever(clever)

I just think it’s asking for trouble to give people ways to hide their failures away and not deal with them. But… de gustibus non est disputandum.


for the dullards in the audience (me) the latin is “There’s no accounting for taste.” which I reckon is spot on in this case

Please sign in to comment.
Something went wrong with that request. Please try again.