diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2012-07-27 12:33:02 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2012-07-27 12:33:02 -0500 |
commit | 04998cd0c92a13332844d04145a4ede3184c7bd0 (patch) | |
tree | 9c2a0727cea530dcfb49a098b56a4c2216c2c86e /activesupport/lib/active_support | |
parent | 99ea1a875b06feb4346869f371e2a57a2cc0a0fc (diff) | |
download | rails-04998cd0c92a13332844d04145a4ede3184c7bd0.tar.gz rails-04998cd0c92a13332844d04145a4ede3184c7bd0.tar.bz2 rails-04998cd0c92a13332844d04145a4ede3184c7bd0.zip |
Add Object#try! with the old NoMethodError raising behavior
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/try.rb | 14 |
1 files changed, 14 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 a2d5caa7df..9974b61078 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -37,6 +37,16 @@ class Object 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 end class NilClass @@ -53,4 +63,8 @@ class NilClass def try(*args) nil end + + def try!(*args) + nil + end end |