Permalink
Browse files

don't raise NoMethodError the tried method doesn't exists

  • Loading branch information...
dmathieu committed May 21, 2011
1 parent f674aed commit 29a5aeaae976bf8432d57ec996c7c81932a39de6
@@ -28,6 +28,8 @@ class Object
def try(*a, &b)
if a.empty? && block_given?
yield self
+ elsif !a.empty? && !respond_to?(a.first)
+ nil
else
__send__(*a, &b)
end
@@ -99,7 +99,7 @@ def setup
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_valid_method

7 comments on commit 29a5aea

@laserlemon

This comment has been minimized.

Show comment
Hide comment
@laserlemon

laserlemon Oct 5, 2011

Contributor

Could somebody please explain this change?

As I understand it, the purpose of Object#try is to call a method on an object of an expected class, but that could also be nil. In what situation would you want to silently avoid a NoMethodError? If I'm trying a method that doesn't exist in my expected class, I should know about it.

Contributor

laserlemon replied Oct 5, 2011

Could somebody please explain this change?

As I understand it, the purpose of Object#try is to call a method on an object of an expected class, but that could also be nil. In what situation would you want to silently avoid a NoMethodError? If I'm trying a method that doesn't exist in my expected class, I should know about it.

@dmathieu

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Oct 5, 2011

Contributor

On STI objects where you might have methods defined for some records, but not for others for example.

Contributor

dmathieu replied Oct 5, 2011

On STI objects where you might have methods defined for some records, but not for others for example.

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Oct 5, 2011

Contributor
Contributor

josevalim replied Oct 5, 2011

@jonleighton

This comment has been minimized.

Show comment
Hide comment
@jonleighton

jonleighton Oct 5, 2011

Member

Agreed, I think we should revert this.

Member

jonleighton replied Oct 5, 2011

Agreed, I think we should revert this.

@alindeman

This comment has been minimized.

Show comment
Hide comment
@alindeman

alindeman Oct 6, 2011

Contributor

-1 to this change as well. It's not acceptable to radically change the behavior at this point.

Contributor

alindeman replied Oct 6, 2011

-1 to this change as well. It's not acceptable to radically change the behavior at this point.

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Oct 6, 2011

Contributor
Contributor

josevalim replied Oct 6, 2011

@arunagw

This comment has been minimized.

Show comment
Hide comment
@arunagw

arunagw Oct 6, 2011

Member

This commit 587dd7d also need to revert the fix the failing test. Or might need a fix!

587dd7d commit is not having test.

Member

arunagw replied Oct 6, 2011

This commit 587dd7d also need to revert the fix the failing test. Or might need a fix!

587dd7d commit is not having test.

Please sign in to comment.