Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 lifo authored
3  actionpack/lib/action_controller/test_process.rb
View
@@ -484,7 +484,8 @@ def method_missing(selector, *args, &block)
#
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
def fixture_file_upload(path, mime_type = nil, binary = false)
- ActionController::TestUploadedFile.new("#{ActionController::TestCase.try(:fixture_path)}#{path}", mime_type, binary)
+ fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
+ ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
end
# A helper to make it easier to test different route configurations.
2  activesupport/lib/active_support/core_ext/object/misc.rb
View
@@ -102,6 +102,6 @@ def acts_like?(duck)
# Person.try(:find, 1)
# @people.try(:map) {|p| p.name}
def try(method, *args, &block)
- send(method, *args, &block) if respond_to?(method, true)
+ send(method, *args, &block) unless self.nil?
end
end
19 activesupport/test/core_ext/object_and_class_ext_test.rb
View
@@ -256,21 +256,13 @@ def setup
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_nil @string.try(method)
+ assert_raises(NoMethodError) { @string.try(method) }
end
def test_valid_method
assert_equal 5, @string.try(:size)
end
- def test_valid_private_method
- class << @string
- private :size
- end
-
- assert_equal 5, @string.try(:size)
- end
-
def test_argument_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
end
@@ -278,4 +270,13 @@ def test_argument_forwarding
def test_block_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
end
+
+ def test_nil_to_type
+ 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
end

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