module Corefines::Object::Blank

@!method blank?

An object is blank if it's +false+, empty, or a whitespace string.
For example, <tt>'', '   ', "\t\n\r", "\u00a0", nil, [], {}</tt> are
all blank.

This simplifies

  address.nil? || address.empty?

to

  address.blank?

@return [Boolean]

@!method presence

Returns object if it's not {#blank?}, otherwise returns +nil+.
+obj.presence+ is equivalent to <tt>obj.blank? ? nil : obj</tt>.

This is handy for any representation of objects where blank is the same
as not present at all. For example, this simplifies a common check for
HTTP POST/query parameters:

  state = params[:state] if params[:state].present?
  country = params[:country] if params[:country].present?
  region = state || country || 'CZ'

becomes...

  region = params[:state].presence || params[:country].presence || 'CZ'

@return [Object, nil] object if it's not {#blank?}, otherwise +nil+.

Constants

BLANK_RE

Public Instance Methods

blank?() click to toggle source
# File lib/corefines/object.rb, line 45
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end
presence() click to toggle source
# File lib/corefines/object.rb, line 49
def presence
  self unless blank?
end