aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object/try.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object/try.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb
index 16a799ec03..9974b61078 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,6 +34,16 @@ class Object
if a.empty? && block_given?
yield self
else
+ public_send(*a, &b) if respond_to?(a.first)
+ end
+ end
+
+ # Same as #try, but will raise a NoMethodError exception if the receiving is not nil and
+ # does not implemented the tried method.
+ def try!(*a, &b)
+ if a.empty? && block_given?
+ yield self
+ else
public_send(*a, &b)
end
end
@@ -50,4 +63,8 @@ class NilClass
def try(*args)
nil
end
+
+ def try!(*args)
+ nil
+ end
end