Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Object#try to use inheritance. [#1774 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
- Loading branch information
1 parent
3b1cd9e
commit 78f2c19
Showing
2 changed files
with
30 additions
and
17 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
class Object | |||
# Tries to send the method only if object responds to it. Return +nil+ otherwise. | |||
# It will also forward any arguments and/or block like Object#send does. | |||
# | |||
# ==== Examples | |||
# | |||
# Without try | |||
# @person && @person.name | |||
# or | |||
# @person ? @person.name : nil | |||
# | |||
# With try | |||
# @person.try(:name) | |||
# | |||
# Try also accepts arguments/blocks for the method it is trying | |||
# Person.try(:find, 1) | |||
# @people.try(:collect) {|p| p.name} | |||
#-- | |||
# This method def is for rdoc only. The alias_method below overrides it as an optimization. | |||
def try(method, *args, &block) | |||
send(method, *args, &block) | |||
end | |||
alias_method :try, :__send__ | |||
end | |||
|
|||
class NilClass | |||
def try(*args) | |||
nil | |||
end | |||
end |
78f2c19
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.
Isn’t the method name and the documentation a bit misleading?
Tries to send the method only if object responds to it. Return nil otherwise.
It would seem that it only works if the object is nil OR responds to the method.
My understanding of it is that it should replace this idiomatic Ruby:
with this
…as first described here: http://ozmm.org/posts/try.html
78f2c19
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.
christos: that's how @#try
used to behave. See this commit and this post for the background.78f2c19
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.
Fair enough. The docs are still not clear though:
Tries to send the method only if object responds to it. Return nil otherwise.
If the method doesn’t respond to the method, it raises NoMethodError
Shouldn’t that be documented?
78f2c19
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.
Christos : Yeah looks like the doc need fixing. I’ve added you to docrails. So maybe you can make that happen :) Just push the doc fix to the docrails branch and it’ll be merged with the rails/master soon.
78f2c19
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.
Pratik: Fixed at http://github.com/lifo/docrails/commit/929088189de1d189c643246a491ba8fb5347b1d3
78f2c19
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.
Hey Chris,
Thanks. I had reverted that commit and applied it with some changes.