Sha256: 58d2cdc4e6a910bf52e232b2d489edb679d8915200eb5a1a69f99a80b3a045a9

Contents?: true

Size: 1.52 KB

Versions: 93

Compression:

Stored size: 1.52 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 !~ /[^[:space:]]/
    # TODO: Opal fails with the previous regex
    self.strip == ''
  end
end

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

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
volt-0.8.15 lib/volt/extra_core/blank.rb
volt-0.8.14 lib/volt/extra_core/blank.rb
volt-0.8.13 lib/volt/extra_core/blank.rb
volt-0.8.11 lib/volt/extra_core/blank.rb
volt-0.8.10 lib/volt/extra_core/blank.rb
volt-0.8.9 lib/volt/extra_core/blank.rb
volt-0.8.8 lib/volt/extra_core/blank.rb
volt-0.8.7 lib/volt/extra_core/blank.rb
volt-0.8.6 lib/volt/extra_core/blank.rb
volt-0.8.5 lib/volt/extra_core/blank.rb
volt-0.8.4 lib/volt/extra_core/blank.rb
volt-0.8.3 lib/volt/extra_core/blank.rb
volt-0.8.2 lib/volt/extra_core/blank.rb
volt-0.8.1 lib/volt/extra_core/blank.rb
volt-0.8.0 lib/volt/extra_core/blank.rb
volt-0.7.23 lib/volt/extra_core/blank.rb
volt-0.7.22 lib/volt/extra_core/blank.rb
volt-0.7.21 lib/volt/extra_core/blank.rb
volt-0.7.20 lib/volt/extra_core/blank.rb
volt-0.7.19 lib/volt/extra_core/blank.rb