Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow optional arguments and/or block for Object#try like Object#send…
… does. [#1425 state:resolved] Original suggestion by Pat Nakajima. Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
- Loading branch information
Showing
2 changed files
with
14 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
823b623
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definition of try would seriously benefit from being implemented like andand, at least from my side of the fence.
Cheers!
-=r
823b623
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rogerdpack is right. The current semantic has many problems.
Firstly the major usecase for try is to catch the case when the receiver is nil, not so much if the method is defined on the receiver. There are also lots of methods on nil, so e.g. object.try(:id) will not archive anything besides subtle errors.
Secondly, because catching the first missing method is not enough, one often sees cascades of two ore more tries:
but (apart from the hard to read syntax) it can hide many problems, because if address is defined one expects to have a reset method on it and otherwise get an exception. So like in andand it would be better to have something like
I guess this form can even be implemented without breaking compatibility with the old form (because the current implementation expects a “method” parameter)
823b623
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it should’ve read
823b623
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the behavior one really wants is
x != nil ? x.send(*args) : nil
which is rather different than the implementation.