I find myself needing to run a method and if the method does not exist, have it return itself, instead of nil quite often, I think this change could be used fairly widely.
module ActiveSupport
module Tryable #:nodoc:
def try(*a, &b)
try!(*a, &b) if a.empty? || respond_to?(a.first)
end
# NEW METHOD
def attempt(*a, &b)
try(*a, &b) || self
end
The best way to argue for an addition like this is to show some before/after code. Preferably a few examples. Then that’ll illustrate how/when this is needed and people can chime in on alternative ways to accomplish the same. Off the top of my head, I can’t remember a single time I’ve wanted something like this in the past 15 years. But a representative example could perhaps jog the memory.
If the before/after cases are compelling, we could then talk about naming etc. Not a big fan of using a synonym in this way to do something so similar and yet subtlety different. But that’s getting ahead of ourselves. First a general need should be clarified.