If string is empty then return some default value
ActiveSupport adds a presence
method to all objects that returns its receiver if present?
(the opposite of blank?
), and nil
otherwise.
Example:
host = config[:host].presence || 'localhost'
Phrogz sort of gave me the idea in PofMagicfingers comment, but what about overriding | instead?
class String
def |(what)
self.strip.blank? ? what : self
end
end
@user.address | "We don't know user's address"