aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2012-07-27 12:20:50 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2012-07-27 12:22:38 -0500
commit99ea1a875b06feb4346869f371e2a57a2cc0a0fc (patch)
tree1f05fdcff4afc50dc39537b50c595fc5acde059e /activesupport
parent3205c768b7c592e5b96fdf6a6dd0fd3c2c9e5775 (diff)
downloadrails-99ea1a875b06feb4346869f371e2a57a2cc0a0fc.tar.gz
rails-99ea1a875b06feb4346869f371e2a57a2cc0a0fc.tar.bz2
rails-99ea1a875b06feb4346869f371e2a57a2cc0a0fc.zip
will now return nil instead of raise a NoMethodError if the receiving object does not implement the method
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md2
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb5
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb8
3 files changed, 10 insertions, 5 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index df144dd00b..0e7a16aa96 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* `Object#try` will now return nil instead of raise a NoMethodError if the receiving object does not implement the method *DHH*
+
* `Time#change` now works with time values with offsets other than UTC or the local time zone. *Andrew White*
* `ActiveSupport::Callbacks`: deprecate usage of filter object with `#before` and `#after` methods as `around` callback. *Bogdan Gusiev*
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
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 98ab82609e..5dd965f405 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -99,13 +99,13 @@ class ObjectTryTest < ActiveSupport::TestCase
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_raise(NoMethodError) { @string.try(method) }
+ assert_nil @string.try(method)
end
def test_nonexisting_method_with_arguments
method = :undefined_method
assert !@string.respond_to?(method)
- assert_raise(NoMethodError) { @string.try(method, 'llo', 'y') }
+ assert_nil @string.try(method, 'llo', 'y')
end
def test_valid_method
@@ -124,7 +124,7 @@ class ObjectTryTest < ActiveSupport::TestCase
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
@@ -148,6 +148,6 @@ class ObjectTryTest < ActiveSupport::TestCase
end
end
- assert_raise(NoMethodError) { klass.new.try(:private_method) }
+ assert_nil klass.new.try(:private_method)
end
end