From 99ea1a875b06feb4346869f371e2a57a2cc0a0fc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 27 Jul 2012 12:20:50 -0500 Subject: will now return nil instead of raise a NoMethodError if the receiving object does not implement the method --- activesupport/lib/active_support/core_ext/object/try.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext/object') diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 16a799ec03..a2d5caa7df 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -5,6 +5,9 @@ class Object # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass. # + # This is also true if the receiving object does not implemented the tried method. It will + # return +nil+ in that case as well. + # # If try is called without a method to call, it will yield any given block with the object. # # Please also note that +try+ is defined on +Object+, therefore it won't work with @@ -31,7 +34,7 @@ class Object if a.empty? && block_given? yield self else - public_send(*a, &b) + public_send(*a, &b) if respond_to?(a.first) end end end -- cgit v1.2.3