Skip to content
This repository
Browse code

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...
commit 5339f813be99012aba01586743d8b24f065e7034 1 parent 296ca4d
Pratik authored
3  actionpack/lib/action_controller/test_process.rb
@@ -484,7 +484,8 @@ def method_missing(selector, *args, &block)
484 484
     #
485 485
     #   post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
486 486
     def fixture_file_upload(path, mime_type = nil, binary = false)
487  
-      ActionController::TestUploadedFile.new("#{ActionController::TestCase.try(:fixture_path)}#{path}", mime_type, binary)
  487
+      fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
  488
+      ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
488 489
     end
489 490
 
490 491
     # 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)
102 102
   # Person.try(:find, 1)
103 103
   # @people.try(:map) {|p| p.name}
104 104
   def try(method, *args, &block)
105  
-    send(method, *args, &block) if respond_to?(method, true)
  105
+    send(method, *args, &block) unless self.nil?
106 106
   end
107 107
 end
19  activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -256,21 +256,13 @@ def setup
256 256
   def test_nonexisting_method
257 257
     method = :undefined_method
258 258
     assert !@string.respond_to?(method)
259  
-    assert_nil @string.try(method)
  259
+    assert_raises(NoMethodError) { @string.try(method) }
260 260
   end
261 261
   
262 262
   def test_valid_method
263 263
     assert_equal 5, @string.try(:size)
264 264
   end
265 265
 
266  
-  def test_valid_private_method
267  
-    class << @string
268  
-      private :size
269  
-    end
270  
-
271  
-    assert_equal 5, @string.try(:size)
272  
-  end
273  
-
274 266
   def test_argument_forwarding
275 267
     assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
276 268
   end
@@ -278,4 +270,13 @@ def test_argument_forwarding
278 270
   def test_block_forwarding
279 271
     assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
280 272
   end
  273
+
  274
+  def test_nil_to_type
  275
+    assert_nil nil.try(:to_s)
  276
+    assert_nil nil.try(:to_i)
  277
+  end
  278
+
  279
+  def test_false_try
  280
+    assert_equal 'false', false.try(:to_s)
  281
+  end
281 282
 end

7 notes on commit 5339f81

Matthew Rudy Jacobs

Hmm… so .try becomes the same as .andand? http://github.com/raganwald/andand

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

gustin prudner

this change was probably “inspired” by this post: http://blog.lawrencepit.com/2009/01/11/try-as-you-might/

Marc-André Lafortune

Great! So much more useful and reliable.

Josh Susser
foo.try(:whatever, clever)

yeah, that’s much more readable than

foo && foo.whatever(clever)
</irony>
Xavier Shay

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

Josh Susser

xaviershay:

(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.

Xavier Shay

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.