Apologies in advance if this has been covered here before, but it's hard to do a search on a term like "try"
First off, I've been thinking that the current behavior of try may not be the most logical:
Compare
nil.to_s
=> ""
vs.
nil.try(:to_s)
=> nil
Would it not make more sense for NilClass to return nil on try only for those methods it doesn't respond to itself? For the vast majority of cases it won't make a difference, but it just seems more logical to me that way.
Secondly, I'd like to suggest an alternate syntax for try, using a proxy object:
i.e.
object.try.method_name
rather than
object.try(:method_name)
I've been using this myself for a while, and I find it more natural than the send style syntax, which I also support for those cases where it's needed.
Incidently, I also set up a not proxy, so that I can do
something.not.nil?
rather than
!something.nil?
Which I find reads better as well.
In any case, I can submit a patch with an of these changes, if there's any interest?
Cheers,
mateo