Class: Object

Inherits:
BasicObject
Defined in:
lib/nutella/core_ext/object/blank.rb,
lib/nutella/core_ext/object/aliases.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns true if the object is false, empty, or a whitespace string.

nil.blank?    # => true
false.blank?  # => true
4.blank?      # => false

Examples:

Compare with and without usage of Object#blank?

# Without:
if str.nil? || str.empty?
# With:
if str.blank?

Returns:

  • (Boolean)

    whether or not the object is blank



15
16
17
# File 'lib/nutella/core_ext/object/blank.rb', line 15

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#presenceObject, NilClass

Returns the object if it is present, returns nil if the object is blank.

"str".presence  # => "str"
"".presence     # => nil

Returns:

  • (Object, NilClass)

    the object if present, otherwise nil



33
34
35
# File 'lib/nutella/core_ext/object/blank.rb', line 33

def presence
  self if present?
end

#present?Boolean

The inverse of Object#blank?.

Returns:

  • (Boolean)

    whether or not the object is not blank



22
23
24
# File 'lib/nutella/core_ext/object/blank.rb', line 22

def present?
  !blank?
end