I originally brought this up in: https://github.com/rails/rails/issues/9067
Rails paved the way for
Object#try…I’d like to propose
Object#tap_if and its counterpart,
I’ve been following 37signals conventions of tapping variables in the views:
<% account.owner.tap do |user| %> ... <% end %>
But, I find myself having to do this a lot…
<% account.owner.tap do |user| %> <% if user %> ... <% end %> <% end %>
It would be great if we could do…
<% account.owner.tap_if(:present?) do |user| %> ... <% end %> <% account.users.tap_if(:any?) do |user| %> ... <% end %>
The block would only yield if the method evals to true.
Carlos mentioned that you can add an “if account.owner.present?” at the end…
But there are times when the account.owner (or something else) call is expensive and you don’t want to call it twice.
Any feedback would be much appreciated. Thanks!