Hello
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.
E.g.
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?
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.