Feature suggestion: Allow passing a block to `presence`, which is run if `present?` is true


I often find myself writing code that checks the presence of some value, e.g. user.posts.first and then passes it to some method if it exists.

Rather than repeating the call to get the value or binding the value to a local variable, I think it would be nice if I could pass a block to presence and then have it run if the value is present.


product.seller.email.presence { |email| schedule_notification(email) }

as opposed to

schedule_notification(product.seller.email) if product.seller.email.present?

This can potentially yield a performance boost in a scenario where each call to seller triggers IO. It also reduces the risk of syntax errors, by eliminating the repeating of product.seller.email.

On the downside it appears to slightly slow down the presence method.

Please see my pull request on GitHub: https://github.com/rails/rails/pull/21470

What do you think?