Sha256: 22cfcaf9e70282ed3f6f2e9dfb07ab1a68e7319edb21513fc7c7105e744f648d

Contents?: true

Size: 1.44 KB

Versions: 56

Compression:

Stored size: 1.44 KB

Contents

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

  # An object is present if it's not <tt>blank?</tt>.
  def present?
    !blank?
  end
end

class NilClass
  # +nil+ is blank:
  #
  #   nil.blank? # => true
  def blank?
    true
  end
end

class FalseClass
  # +false+ is blank:
  #
  #   false.blank? # => true
  def blank?
    true
  end
end

class TrueClass
  # +true+ is not blank:
  #
  #   true.blank? # => false
  def blank?
    false
  end
end

class Array
  # An array is blank if it's empty:
  #
  #   [].blank?      # => true
  #   [1,2,3].blank? # => false
  alias_method :blank?, :empty?
end

class Hash
  # A hash is blank if it's empty:
  #
  #   {}.blank?                # => true
  #   { key: 'value' }.blank?  # => false
  alias_method :blank?, :empty?
end

class String
  # A string is blank if it's empty or contains whitespaces only:
  #
  #   ''.blank?                 # => true
  #   '   '.blank?              # => true
  #   ' '.blank?               # => true
  #   ' something here '.blank? # => false
  def blank?
    self !~ /\S/
  end
end

class Numeric #:nodoc:
  # No number is blank:
  #
  #   1.blank? # => false
  #   0.blank? # => false
  def blank?
    false
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
volt-0.9.5.pre2 lib/volt/extra_core/blank.rb
volt-0.9.5.pre1 lib/volt/extra_core/blank.rb
volt-0.9.4 lib/volt/extra_core/blank.rb
volt-0.9.4.pre5 lib/volt/extra_core/blank.rb
volt-0.9.4.pre3 lib/volt/extra_core/blank.rb
volt-0.9.4.pre2 lib/volt/extra_core/blank.rb
volt-0.9.4.pre1 lib/volt/extra_core/blank.rb
volt-0.9.3 lib/volt/extra_core/blank.rb
volt-0.9.3.pre6 lib/volt/extra_core/blank.rb
volt-0.9.3.pre5 lib/volt/extra_core/blank.rb
volt-0.9.3.pre4 lib/volt/extra_core/blank.rb
volt-0.9.3.pre3 lib/volt/extra_core/blank.rb
volt-0.9.3.pre2 lib/volt/extra_core/blank.rb
volt-0.9.3.pre1 lib/volt/extra_core/blank.rb
volt-0.9.2 lib/volt/extra_core/blank.rb
volt-0.9.1 lib/volt/extra_core/blank.rb
volt-0.9.1.pre5 lib/volt/extra_core/blank.rb
volt-0.9.1.pre4 lib/volt/extra_core/blank.rb
volt-0.9.1.pre3 lib/volt/extra_core/blank.rb
volt-0.9.1.pre2 lib/volt/extra_core/blank.rb