Skip to content

Commit

Permalink
Change Object#try to raise NoMethodError on private methods and alway…
Browse files Browse the repository at this point in the history
…s return nil when Object is nil [Pratik Naik, Lawrence Pit]
  • Loading branch information
lifo committed Jan 13, 2009
1 parent 296ca4d commit 5339f81
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/test_process.rb
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/object/misc.rb
Expand Up @@ -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 changes: 10 additions & 9 deletions activesupport/test/core_ext/object_and_class_ext_test.rb
Expand Up @@ -256,26 +256,27 @@ 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

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

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

@gustin gustin commented on 5339f81 Jan 13, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change was probably “inspired” by this post:
http://blog.lawrencepit.com/2009/01/11/try-as-you-might/

@marcandre
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! So much more useful and reliable.

@joshsusser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foo.try(:whatever, clever)

yeah, that’s much more readable than

foo && foo.whatever(clever)
</irony>

@xaviershay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/foo/big.long.expression/ and it makes sense

@joshsusser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xaviershay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.