Sha256: db09a6551e881aa4536f456384f4185880e9f433a6616ea0e1798d4f0fb5de5a

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true
module ObjectExtensions
  refine Object do
    # An object is blank if it's false, empty, or a whitespace string.
    # For example, +false+, '', '   ', +nil+, [], and {} are all blank.
    #
    # This simplifies
    #
    #   !address || address.empty?
    #
    # to
    #
    #   address.blank?
    #
    # @return [true, false]
    def blank?
      respond_to?(:empty?) ? !!empty? : !self
    end

    # An object is present if it's not blank.
    #
    # @return [true, false]
    def present?
      !blank?
    end
  end
end

module StringExtensions
  refine String do
    BLANK_RE = /\A[[:space:]]*\z/

    # A string is blank if it's empty or contains whitespaces only:
    #
    #   ''.blank?       # => true
    #   '   '.blank?    # => true
    #   "\t\n\r".blank? # => true
    #   ' blah '.blank? # => false
    #
    # Unicode whitespace is supported:
    #
    #   "\u00a0".blank? # => true
    #
    # @return [true, false]
    def blank?
      match BLANK_RE
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finapps_core-2.0.15 lib/core_extensions/object/blank.rb
finapps_core-2.0.14 lib/core_extensions/object/blank.rb