I believe that this also applies to a lot of other use cases,
i.e. only applying some transformation if some condition is true,
but otherwise leaving the result untouched.
I have exactly the same concern as nobu. And that problem stems from the fact that, in this proposal, the condition is given as an argument of the method, which means that it has to be evaluated independently of the return value that appears in the middle of the method chain.
That should take us back to #15557, where the condition is proposed to be given as a block (or a proc, following a suggestion by nobu).
How about calling a condition object if it's callable instead of simply using value as a condition?
class Object
def then_if(condition, &block)
if condition
if (condition.respond_to?(:call) && condition.call(self)) || condition
self.then(&block)
end
else
self
end
end
end